aff*_*fin 5 javascript google-tag-manager
我在标签模板中使用 Google 跟踪代码管理器中的jectScript API,但我无法让它在外部脚本加载(或加载失败)时触发任何回调函数。有什么想法吗?
因此,我的模板具有定义的注入脚本权限: https: //www.google-analytics.com/analytics.js
代码:
// load APIs
const logToConsole = require('logToConsole');
const injectScript = require('injectScript');
const queryPermission = require('queryPermission');
// script url
const url = 'https://www.google-analytics.com/analytics.js';
// Callback to note if script load succeeded
const onSuccess = () => {
logToConsole('Script LOADED');
data.gtmOnSuccess();
};
// Callback to note if script load failed
const onFail = () => {
logToConsole('Script load FAILED.');
data.gtmOnFailure();
};
// Check permission and load script
logToConsole('Checking permission to load script:' + url);
if (queryPermission('inject_script', url)) {
logToConsole('Permission check: OK');
injectScript(url, onSuccess, onFail, url);
} else {
logToConsole('Permission check: FAIL');
data.gtmOnFailure();
}
Run Code Online (Sandbox Code Playgroud)
The log in GTM shows this. Nothing logged from the callback functions for success/fail.
Template preview refreshed at 25/06/2020, 13:15:45
Test started
Checking permission to load script:https://www.google-analytics.com/analytics.js
Permission check: OK
Executed 1 test (SUCCESS)
Run Code Online (Sandbox Code Playgroud)
Any ideas why the callbacks are not happening? I do see analytics.js loading in my chrome console network tab.
I've tried a bunch of variations and sample code of similar examples, and they don't work. I'm running Chrome (Version 83.0.4103.116 (Official Build) (64-bit)). I have Ghostery plugin but its disabled. I also repeated the test in Microsoft Edge and got the same result.
Thanks!
injectScript 提供给DO 的回调函数运行,但logToConsole在这些函数中执行时不会产生日志输出。
因此,如果您依靠查看日志来了解您的函数是否被成功调用以及它们正在做什么,您将不知道发生了什么。
我使用的解决方法是,我不会在这些函数中登录到控制台,而是使用sendPixel我记录的消息作为 GET 参数来调用假 URL。例如www.testabcd12345.com/index.html?msg=my-logged-message
然后我可以在 Chrome 控制台“网络”选项卡中观察这些内容。这很可怕,但已经足够了。
似乎也gtmOnSuccess有类似的限制。