3wcircus / DamnThePotHolesBackend

1 stars 0 forks source link

Google analytics integration throws exception or breaks EJS #48

Closed 3wcircus closed 4 years ago

3wcircus commented 4 years ago

The code suggested for GA causes EJS to throw the dreaded 'no title defined' exception if used as is.

layout.ejs

If comment out datalayer init line EJS works but GA complains datalayer not defined.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-4351854-3"></script>
<script>
    // window.dataLayer = window.dataLayer || []; // This breaks EJS

    function gtag()
    {
        dataLayer.push(arguments);
    }

    gtag('js', new Date());

    gtag('config', 'UA-4351854-3');
</script>
3wcircus commented 4 years ago

I add a conditional in ejs template to not try render title if not defined. Seems to work but likely not ideal.