Google Analytics 在设置时会发出事件吗?

Does Google Analytics emit an event when they are set up?

我们的一些客户希望将他们的 Google 分析 ID 报告给他们。得到它并不难。

ga.getAll()[0].get('clientId');

还有一个排队的语法,这样设置GA的时候就完成了一个操作。

但不想在 GA 准备就绪时发送 GA 事件。准备好后我想问GA一个问题。

也许有什么办法可以对GA说"When you're ready, run this function I'm passing you."

或者,GA 是否会在准备就绪时发出自定义事件?

目前,当 window 发出加载事件时,我的客户正在调用我的代码。但即便如此,也有一小部分时间 ga 未定义。如果您等待几秒钟,它就会被定义。也许是一些奇怪的 cdn 系统。无论如何,我需要一种可靠的方法来确定 GA 何时准备就绪。


(我无法在评论中进行格式化,所以我更改了我的问题以回复 Feathercrown。)

听起来不错!唯一的问题是 ga 还没有定义。这是我自己发送的错误消息示例。
"type": "Page View", "data": { "error": { "message": "ga.getAll is not a function. (In 'ga.getAll()', 'ga.getAll' is undefined)", "message2": "window.GoogleAnalyticsObject = ga", "message3": "typeof ga is not Object" } },

也许 _gxx something 符号已经可用?

只用一个函数调用ga()可以用作回调(ga(readyCallback)),因为函数只会是运行"when the analytics.js library is loaded and ready."

根据the documentation

Invoking the ga() command queue function by passing it a function will schedule the passed function for execution at the next point in the queue.

Since commands are only executed after the analytics.js library is fully loaded, the most common reason to pass the command queue a function is as a callback for when the analytics.js library is loaded and ready.

因此,您可以尝试:

ga(function(){var clientId = ga.getAll()[0].get('clientId');});

或类似的东西。