如何使用 Google Analytics 设置页面标题?
我正在开发的网站的标题结构非常糟糕,并且由于各种原因无法更改。我可以通过 JS 以某种方式“设置”Google Analytics 的页面标题吗?
The site that I'm working on has really crappy title structure and can't be changed for a variety of reasons. Can I "set" the page title for Google Analytics via JS somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
新方式
有一个(当前未记录的功能)允许您覆盖当前页面的标题:
旧方式
Google Analytics 从
document.title
获取标题信息,因此您只需在之前设置 document.title Google Analytics 会运行到您想要的任何值。Chrome 中的测试似乎表明这不会导致标题闪烁,但您的结果可能会有所不同。
The New Way
There's a (currently undocumented feature) that allows you to override the current page's title:
The Old Way
Google Analytics gets the title information from
document.title
, so you could just set document.title before Google Analytics runs to whatever value you want it to be.Tests in Chrome seem to indicate that this doesn't cause a title flicker, but your results may vary.
这在analytics.js(通用分析)中有所不同。
有关详细信息,请访问 Google 开发者网站。
以下是网站的片段:
要发送综合浏览量,您可以向 ga 函数传递一个具有综合浏览量点击类型的发送命令:
执行此命令时,analytics.js 库使用document.title 浏览器属性。
覆盖默认值
如果您需要覆盖默认位置信息,则应直接更新标题和页面值。
要覆盖默认页面值,您可以向 ga 命令传递一个附加参数:
或者,要覆盖这些值,send 命令接受可选字段对象作为最后一个参数。字段对象是标准 JavaScript 对象,但定义了analytics.js 接受的特定字段名称和值。
This is different in analytics.js (Universal Analytics).
Find details on Google's developer site.
Here are Snippets from the Site:
To send a pageview, you pass the ga function a send command with the pageview hit type:
When this command is executed, the analytics.js library sets the title value using the document.title browser property.
Overriding Default Values
If you need to override the default location information, you should update the title and page values directly.
To override the default page value, you can pass the ga command an additional parameter:
Alternatively, to override these values, the send command accepts an optional field object as the last parameter. The field object is a standard JavaScript object, but defines specific field names and values accepted by analytics.js.
您绝对可以在 Google Analytics 中完成此任务。 GA 从 document.title 中提取标题信息,因此您可以在 GA 运行之前将 document.title 设置为您想要的任何值。在要推送的 GA 代码中,您需要设置以下内容:
You can definitely accomplish this in Google Analytics. GA pulls the title information from document.title, so you could just set document.title before GA runs to whatever value you want it to be. In your GA code to push you would set the following: