We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26401
    • 247 Posts
    I’ve posted this elsewhere, but thought I would give it a thread here if anyone is interested

    create a plugin and check "OnWebPagePrerender"
    then in properties assign your google code

    I know it’s very crude, but to those who don’t care it might be good smiley
    it replaces </html> with the code+</html>
    so if there is no html there, there will be no analytics code

    I wanted to add the ability to prevent a loged in user (editor/manager) from generating hits, but couldn’t get it to work, so I gave up.

    enjoy !

    <?php
    /* Handle event */
    if ($modx->event->name === 'OnWebPagePrerender') {
      $jscode = "<script type='text/javascript'>
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount','".$scriptProperties['code']."']);
      _gaq.push(['_trackPageview']);
    
      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>
    </html><!-- $isLoggedIn -->" ;
     $output = &$modx->resource->_output;
      $modx->resource->_output= str_replace("</html>",$jscode,$output);
    }