简体   繁体   English

通过配置变量包含Google的gtag分析

[英]Including Google's gtag analytics via a config variable

I'm trying to setup the new Google analytics, and I'd like to change which GA_TRACKING_ID via a config.js for my Single Page app, depending on whether it is on our dev, staging, or production server. 我正在尝试设置新的Google分析,我想通过config.js为我的单页应用更改哪个GA_TRACKING_ID ,具体取决于它是在我们的开发,登台还是生产服务器上。

We already use this config file to change which API our app hits, and can set the config gtag('config', app.config.analyticsCode); 我们已经使用此配置文件来更改我们的应用程序所访问的API,并可以设置config gtag('config', app.config.analyticsCode); at runtime, but the script to GET the analytics js file seems to hardcode the ID in the request. 在运行时,但获取分析js文件的脚本似乎硬编码请求中的ID。 <script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>

Should I just use JS to write the tag manually? 我应该只使用JS手动编写标签吗? Can I get the script without the tracking ID and initialize it with the gtag() command anyways? 我可以在没有跟踪ID的情况下获取脚本并使用gtag()命令对其进行初始化吗? I couldn't find a way to do this on the Gtag dev docs site. 我无法在Gtag dev docs网站上找到这样做的方法。 Thanks! 谢谢!

It appears it's possible to load the gtag script without defining a default tracking ID, which is what the ?id=GA_TRACKING_ID URL parameter does. 看来可以在不定义默认跟踪ID的情况下加载gtag脚本,这就是?id=GA_TRACKING_ID URL参数。

You can then use: 然后你可以使用:

gtag('config', 'GA_TRACKING_ID'); // replace GA_TRACKING_ID with your tracking ID

to set the default tracking ID for the property. 设置属性的默认跟踪ID。

This is mentioned in the docs : 在文档中提到:

The following code in the snippet: 代码段中的以下代码:

 gtag('config', 'GA_TRACKING_ID'); 

serves these purposes: 用于以下目的:

  • It configures the Google Analytics property with the tracking ID GA_TRACKING_ID as the default property to receive data sent by the event command. 它将跟踪ID GA_TRACKING_ID配置为Google Analytics媒体资源,作为接收事件命令发送的数据的默认属性。

  • It sends a pageview to the default property. 它将页面视图发送到默认属性。

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

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