Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught SyntaxError: Unexpected eval or arguments in strict mode: window.gtag = (arguments) => dataLayer.push(arguments);

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

like image 896
Ricardo Rocha Avatar asked Jan 29 '23 18:01

Ricardo Rocha


1 Answers

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);
like image 73
Bergi Avatar answered Jan 31 '23 07:01

Bergi