title: Google Analytics for Shiny Dashboard App
body: <p>I have a well sorted out R shiny (shinydashboard) app that runs on a server.  I want to be able to track its usage and know that google analytics is a good solution for this.  But I have run into an issue setting it up.</p>
  
  <p>I have tried following the directions described here <a href="https://shiny.rstudio.com/articles/google-analytics.html" rel="nofollow noreferrer">https://shiny.rstudio.com/articles/google-analytics.html</a></p>
  
  <p>They suggest the creation of a google-analytics.js script containing the global site tag from google:</p>
  
  <pre><code>&lt;!-- Global Site Tag (gtag.js) - Google Analytics --&gt;
  &lt;script async src="https://www.googletagmanager.com/gtag/js?id=UA-4XXXXX5-2"&gt;
  &lt;/script&gt;
  &lt;script&gt;
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  
  gtag('config', 'UA-4XXXXX5-2');
  &lt;/script&gt;
  </code></pre>
  
  <p>They then suggest that this "google-analytics.js" script file be called in the shiny app header like the following:</p>
  
  <pre><code>#ui.r
  library(shiny)
  shinyUI(fluidPage(
  
    tags$head(includeScript("google-analytics.js")),
    includeCSS("cerulean.css"),
  
    titlePanel("Sunlight in the US"),
  </code></pre>
  
  <p>However because I am using shiny dashboard my shiny layout is different...</p>
  
  <pre><code>#ui.r
  library(shiny)
  library(shinydashboard)
  
  dashboardPage(
  
    dashboardHeader(title = "Single Cell Database"),
  
    dashboardSidebar(
      sidebarMenu(
        menuItem("P15 Clustering", tabName = "P15_Cluster", icon = icon("th")),
        menuItem("P15 Violin Plots", tabName = "P15_Violin", icon = icon("th"))
      )),
  
    dashboardBody(
      tabItems(
        tabItem(tabName = "P15_Cluster",
  </code></pre>
  
  <p>I can not seem to figure out where to place the...</p>
  
  <pre><code>tags$head(includeScript("google-analytics.js")),
  </code></pre>
  
  <p>... in the shiny dashboard format.  Additionally, because googles code format no longer matches the example i am not confident the new format of script functions.</p>
  
  <p>Any assistance or advice on where to call the "google-analytics.js" script inside the shiny dashboard header, or on how to format the code inside the "google-analytics.js" file would be much appreciated!</p>