Javascript 在本地主机上测试 Google Analytics

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30581916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:25:30  来源:igfitidea点击:

Test Google Analytics on Localhost

javascriptgoogle-analytics

提问by Growler

I need to test some custom firing events using Google Analytics. I'd like to test in a local environment.

我需要使用 Google Analytics 测试一些自定义触发事件。我想在本地环境中测试。

I've set up my test site using apache with server name: my-ga-test.localhost/

我已经使用带有服务器名称的 apache 设置了我的测试站点: my-ga-test.localhost/

According to Can you test google analytics on a localhost address?and Google Analytics docs, I'm able to adjust my GA code to use:

根据你能在本地主机地址上测试谷歌分析吗?Google Analytics 文档,我可以调整我的 GA 代码以使用:

ga('create', 'UA-XXXX-Y', {
  'cookieDomain': 'none'
});

I tried to set up a new account to get a tracking ID for this site, but I'm getting the following error:

我尝试设置一个新帐户以获取此站点的跟踪 ID,但出现以下错误:

URL ends with an invalid top-level domain name

URL 以无效的顶级域名结尾

enter image description here

在此处输入图片说明

How do I generate the code using localhost domain so I can alter the tracking ID code to put on my localhost?

如何使用 localhost 域生成代码,以便我可以更改跟踪 ID 代码以放置在我的 localhost 上?

回答by Philip Walton

The "Website URL" field when creating a new Google Analytics property does not have to be the same as the actual URL that's used in production. It's primarily used for resolving relative links, not for validating hits.

创建新的 Google Analytics(分析)媒体资源时的“网站网址”字段不必与生产中使用的实际网址相同。它主要用于解析相对链接,而不是用于验证命中。

Just change it to something else (like example.com), and everything should work fine.

只需将其更改为其他内容(如example.com),一切都应该正常工作。

回答by JStriedinger

I do not recommend the above answer.

我不推荐上面的答案。

I believe the best practice is to use the Analytics Debug json your localhost. Debug prints in console EVERYTHING is doing, including firing events. That way you can check if your code is working or not without sending tracking info to Analytics on Localhost (which makes no sense).

我相信最佳实践是在本地主机上使用Analytics Debug js。控制台中的调试打印所有正在执行的操作,包括触发事件。这样您就可以检查您的代码是否正常工作,而无需将跟踪信息发送到本地主机上的分析(这是没有意义的)。

Find out more here:)

在此处了解更多信息:)