在闪亮的应用中嵌入Google分析跟踪代码

时间:2021-12-26 14:47:33

I have been using Google analytics to track activity on a shiny web app using the following at the top of my ui.R:

我一直在使用谷歌分析来跟踪闪亮的网络应用程序上的活动,使用我的ui.R顶部的以下内容:

shinyUI(fluidPage(
    tags$head(HTML(
        "<script>
        (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',
        '//www.google-analytics.com/analytics.js','ga');

        ga('create', 'XXXXXXX', 'auto');
        ga('send', 'pageview');

        </script>"
      )),
...
)

However, I recently registered a new app and the tracking code I get from Google analytics is:

但是,我最近注册了一个新应用,我从Google Analytics获得的跟踪代码是:

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

  gtag('config', 'XXXXX');
</script>

Following RStudio's instructions I have saved this as google-analytics.js and inplace of tags$head(HTML("...")) I have:

按照RStudio的说明,我将其保存为google-analytics.js并取代了标签$ head(HTML(“...”))我有:

includeScript("google-analytics.js")

However this does not work. The instructions from the above link say:

但是这不起作用。上述链接的说明如下:

Warning: the includeScript function places the content of the script inside a pair of script tags . If you copy and paste Javascript code into a .js file be sure to remove these tags

警告:includeScript函数将脚本的内容放在一对脚本标记内。如果您将Javascript代码复制并粘贴到.js文件中,请务必删除这些标记

I have experimented with removing <script> and </script>, as well as the html tags surrounding async src="https://www.googletagmanager.com/gtag/js?id=XXXXX" but neither work.

我已经尝试删除

Is there something I'm doing wrong?

有什么我做错了吗?

1 个解决方案

#1


1  

Based on this article by Douglas Watson, all you have to do is:

根据道格拉斯沃森的这篇文章,你所要做的就是:

  1. Copy the analytics HTML snippet into a text file. (created a file named google_analytics.R, in the same directory as ui.R)
  2. 将分析HTML代码段复制到文本文件中。 (创建了一个名为google_analytics.R的文件,与ui.R位于同一目录中)
  3. Include it in ui.R

    将其包含在ui.R中

    shinyUI(fluidPage( tags$head(includeHTML(("google-analytics.html"))), ...

    shinyUI(fluidPage(标签$ head(includeHTML((“google-analytics.html”))),...

#1


1  

Based on this article by Douglas Watson, all you have to do is:

根据道格拉斯沃森的这篇文章,你所要做的就是:

  1. Copy the analytics HTML snippet into a text file. (created a file named google_analytics.R, in the same directory as ui.R)
  2. 将分析HTML代码段复制到文本文件中。 (创建了一个名为google_analytics.R的文件,与ui.R位于同一目录中)
  3. Include it in ui.R

    将其包含在ui.R中

    shinyUI(fluidPage( tags$head(includeHTML(("google-analytics.html"))), ...

    shinyUI(fluidPage(标签$ head(includeHTML((“google-analytics.html”))),...