【发布时间】:2021-07-15 18:54:03
【问题描述】:
我正在尝试将 _gl 的值添加到 URL 以应用跨域。
目前,我尝试了以下选项:
const decorateUrl = (urlString: string) => {
var ga = window.dataLayer;
var tracker;
if (ga && typeof ga.getAll === 'function') {
tracker = ga.getAll()[0]; // Uses the first tracker created on the page
urlString = (new window.gaplugins.Linker(tracker)).decorate(urlString);
}
return urlString;
}
和
const decorateURL = (url: string) => {
let destinationLink = false;
var ga = window[window['GoogleAnalyticsObject']];
console.log(ga,'ga test decorate')
if (ga) {
let tracker = ga.getAll()[0];
let linker = new window.gaplugins.Linker(tracker);
destinationLink = linker.decorate(url);
}
return (destinationLink ? url + '?' + destinationLink : url);
}
但这些都没有将 _gl 代码设置为 URL。
我在标题中注入了这段代码
let gtagLinkScript = destinationWindow.document.createElement("script");
gtagLinkScript.setAttribute('async', 'true');
gtagLinkScript.setAttribute('src', `https://www.googletagmanager.com/gtag/js?id=${ trackingID }`);
和
let gtagAnotherScript = destinationWindow.document.createElement("script");
gtagAnotherScript.innerText = `gtag('set', 'linker', {'domains': [\'${crossDomain}\']});gtag(\'config\', \'${ trackingID }\');`;
有人可以帮忙吗?
【问题讨论】:
-
你能添加你用来调用这些函数的代码吗?例如,element.addEventListener....等
-
if (!window.gtag) { const appGTAGWindowScript = createGTAGWindowScript( window, appConfig.analytics.gtagNewVersionTrackingCode, appConfig.analytics.crossDomain ); operationWindow.document.head.appendChild(appGTAGLinkScript); operationWindow.document.head.appendChild(appGTAGWindowScript); }
-
在loader文件中,在header中调用