mercredi 9 août 2017

Is it fine if I put a conditional if else statement on my google analytics script?

I tested my tracking code via console and it's already working but I haven't tested it on live server. I just want to ask if this would work?

Here's my sample GA tracking code:

<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','http://ift.tt/1pxBd1V','ga');


    $(document).ready(function () {
        var $url = $(location).attr('href');

        if ($url.indexOf('https://test.com/') > -1) {
            alert('TEST first');
            ga('create', 'UA-XXXXXXXX-6', 'auto');
            ga('send', 'pageview');

        } else if ($url.indexOf('https://test2.com/') > -1) {
            alert('TEST second');
            ga('create', 'UA-XXXXXXXX-7', 'auto');
            ga('send', 'pageview');

        } else if ($url.indexOf('https://test3.com/') > -1) {
            alert('TEST third');
            ga('create', 'UA-XXXXXXXX-8', 'auto');
            ga('send', 'pageview');

        } else {
            alert('TEST fourth');
            ga('create', 'UA-XXXXXXXX-3', 'auto');
            ga('send', 'pageview');
        }
    });


</script>

Will update once it worked.

Aucun commentaire:

Enregistrer un commentaire