2017-10-04 164 views
2

我有一个很好的整理R闪亮(shinydashboard)应用程序在服务器上运行。我希望能够跟踪其使用情况,并知道谷歌分析是一个很好的解决方案。但是我遇到了一个设置它的问题。谷歌分析闪亮仪表板应用程序

我试图在这里描述https://shiny.rstudio.com/articles/google-analytics.html

他们认为含有从谷歌全球网站标签的谷歌,analytics.js的脚本的创作方向如下:

<!-- 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> 

然后,他们认为,这种“ google-analytics.js“脚本文件在闪亮的应用程序标题中调用,如下所示:

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

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

    titlePanel("Sunlight in the US"), 

但是因为我正在使用shin Ÿ仪表盘我的闪亮布局不同...

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

我似乎无法弄清楚在何处放置...

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

......在闪亮的仪表盘格式。另外,由于googles代码格式不再与示例相匹配,我不相信脚本函数的新格式。

任何有关如何在闪亮的仪表板标题中调用“google-analytics.js”脚本或如何格式化“google-analytics.js”文件中的代码的帮助或建议将非常感谢!

回答

1

我和你有同样的问题。我可以解决这个问题,并在之后https://shiny.rstudio.com/articles/usage-metrics.html

第一步教程之后的工作原理:您可以使用下面的代码在谷歌,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'); 
}); 

;第二,你可以调用在“dashboardBody”中输入“google-analytics.js”。如下面的语法:

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