javascript 如何使用 Google Analytics 设置页面标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7322288/
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
How can I set a Page Title with Google Analytics?
提问by Shamoon
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?
我正在处理的站点的标题结构非常糟糕,由于各种原因无法更改。我可以通过 JS 以某种方式“设置”Google Analytics 的页面标题吗?
回答by Yahel
The New Way
新方式
There's a (currently undocumented feature) that allows you to override the current page's title:
有一个(当前未记录的功能)允许您覆盖当前页面的标题:
_gaq.push(["_set", "title", "Your Brand New Page Title"]);
_gaq.push(["_trackPageview"]); //will send with the overridden page 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.
Google Analytics 从 获取标题信息document.title
,因此您可以在 Google Analytics 运行之前将 document.title 设置为您想要的任何值。
_gaq.push(function(){
var oldtitle = document.title;
document.title = "More Descriptive Title";
_gaq.push(["_trackPageview"]);
document.title = oldtitle;
});
Tests in Chrome seem to indicate that this doesn't cause a title flicker, but your results may vary.
Chrome 中的测试似乎表明这不会导致标题闪烁,但您的结果可能会有所不同。
回答by Dave
This is different in analytics.js (Universal Analytics).
这在 analytics.js(通用分析)中有所不同。
Find details on Google's developer site.
在Google 的开发者网站上查找详细信息。
Here are Snippets from the Site:
以下是该网站的片段:
To send a pageview, you pass the ga function a send command with the pageview hit type:
要发送综合浏览量,您需要向 ga 函数传递一个具有综合浏览量命中类型的发送命令:
ga('send', 'pageview');
When this command is executed, the analytics.js library sets the title value using the document.title browser property.
执行此命令时,analytics.js 库使用 document.title 浏览器属性设置标题值。
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:
要覆盖默认页面值,您可以向 ga 命令传递一个附加参数:
ga('send', 'pageview', '/my-overridden-page?id=1');
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.
或者,要覆盖这些值,发送命令接受一个可选字段对象作为最后一个参数。field 对象是一个标准的 JavaScript 对象,但定义了由 analytics.js 接受的特定字段名称和值。
ga('send', 'pageview', {
'page': '/my-overridden-page?id=1',
'title': 'my overridden page'
});
回答by Stratigent
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:
您绝对可以在 Google Analytics 中完成此操作。GA 从 document.title 中提取标题信息,因此您可以在 GA 运行之前将 document.title 设置为您想要的任何值。在要推送的 GA 代码中,您将设置以下内容:
_gaq.push(["_set", "title", "Your Brand New Page Title"]);
_gaq.push(["_trackPageview"]); //will send with the overridden page title