2017-10-04 4 views
2

J'ai une application R shiny (shinydashboard) qui fonctionne sur un serveur. Je veux pouvoir suivre son utilisation et savoir que google analytics est une bonne solution pour cela. Mais j'ai rencontré un problème de mise en place.Application Google Analytics pour Shiny Dashboard

J'ai essayé en suivant les instructions décrites ici https://shiny.rstudio.com/articles/google-analytics.html

Ils suggèrent la création d'un script google-analytics.js contenant la balise globale du site de google:

<!-- Global Site Tag (gtag.js) - Google Analytics --> 
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-4XXXXX5-2"> 
</script> 
<script> 
window.dataLayer = window.dataLayer || []; 
function gtag(){dataLayer.push(arguments);} 
gtag('js', new Date()); 

gtag('config', 'UA-4XXXXX5-2'); 
</script> 

Ils suggèrent ensuite que cette " fichier script google-analytics.js » être appelé dans l'en-tête d'application brillante comme ce qui suit:

#ui.r 
library(shiny) 
shinyUI(fluidPage(

    tags$head(includeScript("google-analytics.js")), 
    includeCSS("cerulean.css"), 

    titlePanel("Sunlight in the US"), 

Cependant, parce que je suis en utilisant tibia y dashboard ma mise en page brillante est différent ...

#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", 

Je ne peux pas à comprendre où placer la ...

tags$head(includeScript("google-analytics.js")), 

... dans le format du tableau de bord brillant. En outre, parce que le format de code googles ne correspond plus à l'exemple, je ne suis pas sûr du nouveau format des fonctions de script.

Toute aide ou conseil sur l'endroit où appeler le script "google-analytics.js" dans l'en-tête de tableau de bord brillant, ou sur la façon de formater le code dans le fichier "google-analytics.js" serait très appréciée!

Répondre

1

J'ai le même problème avec vous. Je peux résoudre le problème et il fonctionne après avoir suivi le tutoriel à https://shiny.rstudio.com/articles/usage-metrics.html

première étape: Vous pouvez utiliser le code suivant dans le fichier google-analytics.js:

(function(i,s,o,g,r,a,m){ 
    i['GoogleAnalyticsObject']=r; 
    i[r]=i[r] || 
    function(){ 
    (i[r].q=i[r].q||[]).push(arguments); 
    },i[r].l=1*new Date(); 
    a=s.createElement(o), 
    m=s.getElementsByTagName(o)[0]; 
    a.async=1; 
    a.src=g; 
    m.parentNode.insertBefore(a,m); 
})(window,document,'script', 
    'https://www.google-analytics.com/analytics.js','ga'); 
ga('create', 'UA-4XXXXX5-2', 'auto'); 
ga('send', 'pageview'); 

$(document).on('change', 'select', function(e) { 
    ga('send', 'event', 'widget', 'select data', $(e.currentTarget).val()); 
}); 

$(document).on('click', 'button', function() { 
    ga('send', 'event', 'button', 'plot data'); 
}); 

et le second, vous pouvez appeler le fichier "google-analytics.js" dans "dashboardBody". telle que la syntaxe ci-dessous:

dashboardBody(
    tags$head(includeScript("google-analytics.js")), 
    tabItems(
     tabItem(tabName = "P15_Cluster",