简体   繁体   English

使用Meteor应用程序设置Google Analytics(分析):未安装跟踪错误

[英]Setup Google Analytics with Meteor App: Tracking not Installed Error

I am Trying to set up Google Analytics with Meteor and always get a Tracking not installed Error. 我正在尝试使用Meteor设置Google Analytics(分析),并且总是出现“未安装跟踪”错误。

I have already tried to use the meteor Packets, I have tried to include the tracking code in the Head Section, in the Body section and as a rendered Callback on a template. 我已经尝试使用流星数据包,尝试将跟踪代码包括在Head部分,Body部分以及作为模板上的呈现回调。

I always get the error from the Google Analytics Site "Tracking not Installed" 我总是从Google Analytics(分析)网站收到“未安装跟踪”错误

What is the best practice of GA tracking in Meteor Apps? 在Meteor Apps中进行GA跟踪的最佳做法是什么?

You don't need a package for GA, probably easier to do it yourself, like this: 您不需要用于GA的程序包,可能自己更容易完成,例如:

First, put the GA tracker id in Meteor.settings.public. 首先,将GA跟踪器ID放入Meteor.settings.public。 (more info about that here ) (有关此的更多信息)

Then, create a file for the client, probably in the lib folder, for example /client/lib/google_analytics.js and add the regular GA tracker to it: 然后,为客户端创建一个文件,可能在lib文件夹中,例如/client/lib/google_analytics.js然后向其中添加常规GA跟踪器:

// CLIENT
/*****************************************************************************/
/* Google Analytics */
/*****************************************************************************/
if (Meteor.settings.public.GaTrackingId) {

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

}

Last, create another file to track the page load. 最后,创建另一个文件来跟踪页面加载。 You can do this in a Meteor.startup() method, but you don't have to as the tracker code is in /lib which is loaded before everything else. 您可以在Meteor.startup()方法中执行此操作,但不必这样做,因为跟踪器代码位于/ lib中,该代码先于其他加载。

// CLIENT
if (Meteor.settings.public.GaTrackingId) {

  ga('create', Meteor.settings.public.GaTrackingId, 'auto');
  ga('send', 'pageview');
}

That's it. 而已。 Now you can also do GA event tracking, etc. from anywhere in your app if you'd like to. 现在,您还可以根据需要在应用程序中的任何位置进行GA事件跟踪等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM