Google Analytics for Shiny Dashboard App

时光总嘲笑我的痴心妄想 提交于 2019-12-02 00:49:47

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

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');
});

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",
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!