'What is the `js` gtags.js command?
The embed code for Google Analytics (well, GA via google tag manager's gtags.js) looks like:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=REDACTED"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'REDACTED');
</script>
I'm building an SPA app, and I'd like to know what I need to do a virtual pageview - Google's documentation leaves out anything about the js
command, and I don't want to leave any stones unturned...
- What is the line
gtag('js', new Date());
doing? - If I tell the
config
command in the snippet to not register a pageview, and then callgtags('config', 'REDACTED', {...})
later (with page info in the{...}
), do I need to executegtag('js', new Date());
before the config call?
Solution 1:[1]
This is the relevant code from the gtag JavaScript file:
js: function(a) {
if (2 == a.length && a[1].getTime) return {
event: "gtm.js",
"gtm.start": a[1].getTime()
}
}
It would seem that this is an init function - it checks that some sort of command array exists and has an expected length (probably to make sure it hasn' been initialized before) and then emits the gtm.js event and the timestamp (a[1].getTime
checks for the Date object that has been passed in by testing for a getTime method).
So it probably does neither harm nor good to run it multiple times (since it will return a values only once).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |