[英]Google Analytics for Shiny Dashboard App
I have a well sorted out R shiny (shinydashboard) app that runs on a server. 我有一个运行良好的R闪亮(shinydashboard)应用程序,可以在服务器上运行。 I want to be able to track its usage and know that google analytics is a good solution for this. 我希望能够跟踪其使用情况,并知道Google Analytics(分析)是一个很好的解决方案。 But I have run into an issue setting it up. 但是我遇到了设置它的问题。
I have tried following the directions described here https://shiny.rstudio.com/articles/google-analytics.html 我已经尝试按照此处描述的指示进行操作https://shiny.rstudio.com/articles/google-analytics.html
They suggest the creation of a google-analytics.js script containing the global site tag from google: 他们建议创建一个google-analytics.js脚本,其中包含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>
They then suggest that this "google-analytics.js" script file be called in the shiny app header like the following: 然后,他们建议在闪亮的应用程序标题中调用此“ google-analytics.js”脚本文件,如下所示:
#ui.r
library(shiny)
shinyUI(fluidPage(
tags$head(includeScript("google-analytics.js")),
includeCSS("cerulean.css"),
titlePanel("Sunlight in the US"),
However because I am using shiny dashboard my shiny layout is different... 但是,因为我使用的是闪亮的仪表板,所以我的闪亮布局有所不同...
#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",
I can not seem to figure out where to place the... 我似乎无法弄清楚放置...的位置
tags$head(includeScript("google-analytics.js")),
... 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. 此外,由于googles代码格式不再与示例匹配,因此我不确定脚本函数的新格式。
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! 对于在闪亮的仪表板标题内何处调用“ google-analytics.js”脚本或在“ google-analytics.js”文件内如何格式化代码的任何帮助或建议,将不胜感激!
I have the same problem with you. 我也有同样的问题 I can solve the problem and it works after following the tutorial at https://shiny.rstudio.com/articles/usage-metrics.html 我可以解决问题,并且可以通过按照https://shiny.rstudio.com/articles/usage-metrics.html上的教程进行操作
first step: You can use the following code in google-analytics.js file : 第一步:您可以在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');
});
and the second, you can call the file "google-analytics.js" in "dashboardBody". 第二,您可以在“ dashboardBody”中调用文件“ google-analytics.js”。 such as the syntax below: 例如以下语法:
dashboardBody(
tags$head(includeScript("google-analytics.js")),
tabItems(
tabItem(tabName = "P15_Cluster",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.