Google Analytics 创建/更新其所有 cookie 后立即执行 Javascript 函数

发布于 2024-12-09 23:41:42 字数 195 浏览 2 评论 0原文

我们如何让网页在 Google Analytics 创建/更新其所有 Cookie 并加载页面 DOM 后立即执行 JavaScript 函数。

可以用 jQuery 来完成吗?如何?

此函数从 GA cookie 获取信息。我尝试在加载页面 DOM 时执行该函数,但有时 GA 尚未创建它的 cookies。

How do we make a web page to execute a JavaScript function immediately after Google Analytics creates/updates all its cookies and the page DOM is loaded.

Can it be done with jQuery? How?

This function gets information from the GA cookies. I've tried executing the function when the page DOM is loaded, but sometimes GA has not created it's cookies yet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

云淡风轻 2024-12-16 23:41:42

这里有两个要素:首先,DOM 与 jQuery 做好了准备。这很简单:

$(document).ready(function(){
//do something
});

确保函数在 Google Analytics 创建 cookie 之后执行的方法(一旦隐式执行 initData())就是在 中传递该函数。 code>_gaq 数组队列,以便在 _trackPageview 调用之后执行。

例如:

_gaq.push(["_trackPageview"]);
_gaq.push(function(){
    //when this executes, the google analytics cookies definitely exist
});

因此,您可以将以下内容放在页面上 _gaq.push(["_trackPageview"]) 下面的任何位置(或者在任何启动 cookie 的 _gaq 调用之后;最常见的是 _trackPageview_trackEvent_setCustomVar

_gaq.push(function(){
    //do something
});

现在确保函数两者在 DOM Ready 后执行 。 并且隐式调用 initData() ,您可以这样做:

_gaq.push(function(){
    $(document).ready(function(){
        //do something
    });
});

或者:

$(document).ready(function(){
    _gaq.push(function(){
        //do something
   });
});

它们都执行您正在寻找的操作,尽管听起来第一个更适合你想要完成的事情的心智模型。

There are 2 elements here: First, DOM ready with jQuery. That's easy:

$(document).ready(function(){
//do something
});

The way to ensure that your function executes after Google Analytics has created its cookies (once it had implicitly executed initData()) is to pass the function within the _gaq array queue so that it gets executed after the _trackPageview call.

For example:

_gaq.push(["_trackPageview"]);
_gaq.push(function(){
    //when this executes, the google analytics cookies definitely exist
});

So, you could put the following anywhere on the page below _gaq.push(["_trackPageview"]) (or after any _gaq call that initiates the cookies; most commonly, _trackPageview, _trackEvent, and _setCustomVar.

_gaq.push(function(){
    //do something
});

Now to ensure that the function both executes after DOM Ready and that initData() is implicitly called, you can either do this:

_gaq.push(function(){
    $(document).ready(function(){
        //do something
    });
});

Or:

$(document).ready(function(){
    _gaq.push(function(){
        //do something
   });
});

They both do what you're looking for, though it sounds like the first one better fits your mental model of what you're trying to accomplish.

做个少女永远怀春 2024-12-16 23:41:42

要完成/更新@Yahel的答案,关于语法,您现在应该编写:

ga(function(tracker) {
  console.log(tracker.get('clientId'));
});

来源:developers.google.com

To complete/update @Yahel's answer, concerning the syntax, you should now write:

ga(function(tracker) {
  console.log(tracker.get('clientId'));
});

Source: developers.google.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文