So what I read from this code, a callback function needs to be added to the dataLayer array. It seems like this callback function is supposed to be called from GA at some time.
The problem is, that function is never called. And I cannot find any references on the web that mention adding a function to that array. Everybody just puts objects in there, with keys and some values.
So none of my script runs in the first place. Can you please explain how this thing starts? I'm only interested in Google Analytics with analytics.js, nothing else.
Here's my code:
window.dataLayer = window.dataLayer || [];
function removeUtmParams() {
console.log("removeUtmParams called");
// Only do this if the browser supports changing the URL of the page
if (window.history.replaceState) {
// Wait for the analytics library to be loaded, or the timeout
let retries = 200;
let pollInterval;
pollInterval = setInterval(function () {
if (retries-- < 0 || window[window.GoogleAnalyticsObject]) {
clearInterval(pollInterval);
// Then push a function onto its queue
console.log("polling completed, retries:", retries);
window[window.GoogleAnalyticsObject](function () {
console.log("cleaning URL");
// Now the tracking is done, clean up the URL
let cleanSearch = window.location.search.
replace(/utm_[^&]+&?/g, "").
replace(/&$/, "").
replace(/^\?$/, "");
let cleanUrl = window.location.pathname + cleanSearch + window.location.hash;
window.history.replaceState(window.history.state, "", cleanUrl);
});
}
}, 50);
}
}
function gtag() {
dataLayer.push(arguments);
}
gtag("js", new Date());
gtag("config", "UA-.....", { "anonymize_ip": true });
console.log("pushing to dataLayer");
gtag(removeUtmParams);
The log says "pushing to dataLayer" and nothing else. The URL is untouched. The page view is tracked in the realtime view.
PS: When I call removeUtmParams directly instead of adding it to the array, it runs to completion, cleans the URL, but I can't see the source value in the realtime reports. But then again, I can't even see the source in the report when I don't clean up the URL. Strange.
So what I read from this code, a callback function needs to be added to the
dataLayer
array. It seems like this callback function is supposed to be called from GA at some time.The problem is, that function is never called. And I cannot find any references on the web that mention adding a function to that array. Everybody just puts objects in there, with keys and some values.
So none of my script runs in the first place. Can you please explain how this thing starts? I'm only interested in Google Analytics with analytics.js, nothing else.
Here's my code:
The log says "pushing to dataLayer" and nothing else. The URL is untouched. The page view is tracked in the realtime view.
PS: When I call
removeUtmParams
directly instead of adding it to the array, it runs to completion, cleans the URL, but I can't see the source value in the realtime reports. But then again, I can't even see the source in the report when I don't clean up the URL. Strange.