I have the following code:
class MetricGoogleGateway extends AMetricGateway{
constructor(id, name, token) {
super(id, name);
this.token = token;
}
configure() {
if(!window.dataLayer && !window.gtag)
{
window.dataLayer = window.dataLayer || [];
window.gtag = (arguments) => dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-113071675-1');
}
}
When the file is loaded by the browser, I got the following error:
Uncaught SyntaxError: Unexpected eval or arguments in strict mode
But if I I run the following lines in the chrome console, everything its ok:
window.dataLayer = window.dataLayer || [];
window.gtag = (arguments) => dataLayer.push(arguments);
window.gtag(1);
window.gtag(2);
console.log(window.dataLayer)
-> console.log result: [1,2]
NOTE:
I notice that if I changed the line:
window.gtag = (arguments) => dataLayer.push(arguments);
For the line:
window.gtag = function(arguments) { dataLayer.push(arguments) }
I get the same error
Your code is inside a class
body which runs in strict mode (as all your code should), unlike the snippet pasted into the console.
The names arguments
and eval
are special and forbidden as identifiers. Don't use them. Just write args
instead, or even better something actually meanigful like layer
.
window.gtag = (layer) => dataLayer.push(layer);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With