如何使用 Google Analytics 设置页面标题?

发布于 2024-12-03 05:22:19 字数 80 浏览 1 评论 0原文

我正在开发的网站的标题结构非常糟糕,并且由于各种原因无法更改。我可以通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈下烟灰 2024-12-10 05:22:19

新方式

有一个(当前未记录的功能)允许您覆盖当前页面的标题:

_gaq.push(["_set", "title", "Your Brand New Page Title"]);
_gaq.push(["_trackPageview"]); //will send with the overridden page title

旧方式

Google Analytics 从 document.title 获取标题信息,因此您只需在之前设置 document.title Google Analytics 会运行到您想要的任何值。

_gaq.push(function(){
    var oldtitle = document.title; 
    document.title = "More Descriptive Title";
    _gaq.push(["_trackPageview"]);
    document.title = oldtitle;
});

Chrome 中的测试似乎表明这不会导致标题闪烁,但您的结果可能会有所不同。

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.

_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.

演出会有结束 2024-12-10 05:22:19

这在analytics.js(通用分析)中有所不同。

有关详细信息,请访问 Google 开发者网站

以下是网站的片段:

要发送综合浏览量,您可以向 ga 函数传递一个具有综合浏览量点击类型的发送命令:

ga('send', 'pageview');

执行此命令时,analytics.js 库使用document.title 浏览器属性。

覆盖默认值

如果您需要覆盖默认位置信息,则应直接更新标题和页面值。

要覆盖默认页面值,您可以向 ga 命令传递一个附加参数:

ga('send', 'pageview', '/my-overridden-page?id=1');

或者,要覆盖这些值,send 命令接受可选字段对象作为最后一个参数。字段对象是标准 JavaScript 对象,但定义了analytics.js 接受的特定字段名称和值。

    ga('send', 'pageview', {
  'page': '/my-overridden-page?id=1',
  'title': 'my overridden page'
});

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:

ga('send', 'pageview');

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:

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.

    ga('send', 'pageview', {
  'page': '/my-overridden-page?id=1',
  'title': 'my overridden page'
});
遇到 2024-12-10 05:22:19

您绝对可以在 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 

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:

_gaq.push(["_set", "title", "Your Brand New Page Title"]);
_gaq.push(["_trackPageview"]); //will send with the overridden page title 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文