Search code examples
javascriptgoogle-analyticssyntax-error

Google Analytics OptOut Uncaught SyntaxError


I tried to implement the GoogleAnalytics Optout possibillity, because the german dataprotection law forces me to do that.

I use this code:

<script type="text/javascript">
var gaProperty = 'UA-XXXXXXXXX-X';
var disableStr = 'ga-disable-' + gaProperty;

if (document.cookie.indexOf(disableStr + '=true') > -1) {
    window[disableStr] = true;
}

function gaOptout() {
    document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;
    path=/';
    window[disableStr] = true;
}
</script>

If I look into the chrome developer console I get this error:

Uncaught SyntaxError: Invalid or unexpected token

The error is in this line

function gaOptout() {

and marked is the {

I tried it a few times allready and sometimes it is this bracket and sometimes it is an other {.

What is wrong? Any ideas? I'm searching a half day and tried so many solutions from the web. But nothing works...

If it is important I implement google analytics with a hard coded Global Site Tag (gtag)


Solution

  • Solution was the "enter" behind the ; in line

    document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;
    

    it should be

    document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC;path=/';