用于闪亮仪表板应用程序的 Google Analytics [英] Google Analytics for Shiny Dashboard App

查看:20
本文介绍了用于闪亮仪表板应用程序的 Google Analytics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我已尝试按照此处描述的说明进行操作 https://shiny.rstudio.com/articles/google-analytics.html

I have tried following the directions described here https://shiny.rstudio.com/articles/google-analytics.html

他们建议创建一个 google-analytics.js 脚本,其中包含来自 google 的全局站点标签:

They suggest the creation of a google-analytics.js script containing the global site tag from 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>

然后他们建议在闪亮的应用标头中调用这个google-analytics.js"脚本文件,如下所示:

They then suggest that this "google-analytics.js" script file be called in the shiny app header like the following:

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

... 闪亮的仪表盘格式.此外,由于 googles 代码格式不再与示例匹配,我对脚本函数的新格式没有信心.

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

任何有关在闪亮的仪表板标题中调用google-analytics.js"脚本的位置或如何格式化google-analytics.js"文件中的代码的任何帮助或建议,将不胜感激!

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!

推荐答案

我和你有同样的问题.我可以解决这个问题,并且在按照 https://shiny.rstudio 上的教程进行操作后它可以工作.com/articles/usage-metrics.html

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

第一步:你可以在google-analytics.js文件中使用以下代码:

first step: You can use the following code in google-analytics.js file :

(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".比如下面的语法:

and the second, you can call the file "google-analytics.js" in "dashboardBody". such as the syntax below:

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

这篇关于用于闪亮仪表板应用程序的 Google Analytics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆