Google Analytics 创建/更新其所有 cookie 后立即执行 Javascript 函数
我们如何让网页在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个要素:首先,DOM 与 jQuery 做好了准备。这很简单:
确保函数在 Google Analytics 创建 cookie 之后执行的方法(一旦隐式执行
initData()
)就是在 中传递该函数。 code>_gaq 数组队列,以便在_trackPageview
调用之后执行。例如:
因此,您可以将以下内容放在页面上
_gaq.push(["_trackPageview"])
下面的任何位置(或者在任何启动 cookie 的_gaq
调用之后;最常见的是_trackPageview
、_trackEvent
和_setCustomVar
现在确保函数两者在 DOM Ready 后执行 。 并且隐式调用
initData()
,您可以这样做:或者:
它们都执行您正在寻找的操作,尽管听起来第一个更适合你想要完成的事情的心智模型。
There are 2 elements here: First, DOM ready with jQuery. That's easy:
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:
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
.Now to ensure that the function both executes after DOM Ready and that
initData()
is implicitly called, you can either do this:Or:
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.
要完成/更新@Yahel的答案,关于语法,您现在应该编写:
来源:developers.google.com
To complete/update @Yahel's answer, concerning the syntax, you should now write:
Source: developers.google.com