Search code examples
javascriptphpgoogle-analyticssmartyuniversal-analytics

Google Universal Analytics tracking code generating an empty page


It is the first time i am using the new version of Google Analytics (Universal Analytics), so when i put the below code imediatally before </head> i get blank pages for all my URLs.

Here is the code given by Google:

<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','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-1234567-89', 'mysite.com');
  ga('send', 'pageview');

</script>

Someone knows why it is happening? Seems to be a bug, but i don't know how to solve this.


Solution

  • Based on @Brad comment i detected that PHP Smarty was crashing with Universal Analytics code tracking, so i found this post:

    Use {Literal} Smarty Tag to Add Google Analytics Tracking Code to BlogSome Hosted Blogs

    And then i did like below and the tracking code works perfectly!

    <script>
    
        {literal}
    
        (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','//www.google-analytics.com/analytics.js','ga');
    
        ga('create', 'UA-1234567-89', 'mysite.com');
        ga('send', 'pageview');
    
        {/literal}
    
    </script>