We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Turn it on and forget it. Works for your site that has pages checked to log visits in the Manager.

    This is from Mark Kaplan, who had to go to bed so he’d be fresh for his finals tomorrow. wink

    <?php
    // Google Analytics Plugin by Mark Kaplan
    // 31-Jan-2006 
    // version 1.0
    //
    // Adds your Google Analytics to every page in your site
    //
    // Parameter: &account=Account;string;UA-000000-0 
    // Event: OnWebPagePrerender 
     
    $e = $modx->Event; 
    $account = isset($account)? $account: '';
    $script = '
    <script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script> 
    <script type="text/javascript"> 
        // Google Site tracking 
        _uacct = "'.$account.'"; 
        urchinTracker(); 
    </script>
    '; 
    
    switch ($e->name) { 
    	case "OnWebPagePrerender": 
    		$googleize = ($modx->documentObject['donthit']==0 && $modx->documentObject['contentType']=='text/html');
                  if ($googleize) { 
    			$modx->documentContent = preg_replace("/(<\/head>)/i", $script."\n\\1", $modx->documentContent); 
    		}
    		break;
    
    	default : 
    		return; // stop here - this is very important. 
    		break; 
    }
    ?>
    



    Feedback appreciated!
      Ryan Thrash, MODX Co-Founder
      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • 6726
      • 7,075 Posts
      I’ll replace the Google Analytics snippet by this plugin and report smiley

      (Should the snippet be taken off download to avoid confusion, as far as support is concerned ?)

      Thanks !
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l&#39;outil id
      • I think there’s a place for both a plugin and a snippet. Some people might want one or the other. Or to manage GA, by including the snippet in a header chunk or TV, for instance.

        It’s about flexibility! We typically have 2-3 ways to get to the same end result for just about everything it seems... wink
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 6726
          • 7,075 Posts
          That’s true and I am one big fan of that, just that the code is so similar except the OnWebPagePrerender thing...

          At least this will make people aware that there was also a snippet smiley
            .: COO - Commerce Guys - Community Driven Innovation :.


            MODx est l&#39;outil id
            • 32241
            • 1,495 Posts
            Hemm.. Do we have to have a secial user account on google to obtain google analytic? I never use it, but if someone can assist me with a descrption why it’s better than the other stats, it will be awesome.

            Thanks
              Wendy Novianto
              [font=Verdana]PT DJAMOER Technology Media
              [font=Verdana]Xituz Media
              • 1764
              • 680 Posts
              One small suggestion. I think it would be a good idea to check the content type so that it doesn’t get used for plain-text, xml, js, css or any other document that isn’t text/html.

              I think that changing these two lines just below case "OnWebPagePrerender" : should do it

              <?php
              $googleize = ($modx->documentObject['donthit']==0 && $modx->documentObject['contentType']=='text/html');
              if ($googleize) {  
              ?>
              


              I haven’t tested this though.
              • Hmmm... I’m getting a duplicate entries of MODX_MEDIA_PATH:

                <script type="text/javascript">var MODX_MEDIA_PATH = "media";</script>
                <script type="text/javascript">var MODX_MEDIA_PATH = "media";</script>
                
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 4018
                  • 1,131 Posts
                  Quote from: rthrash at Feb 01, 2006, 06:02 PM

                  Hmmm... I’m getting a duplicate entries of MODX_MEDIA_PATH:

                  <script type="text/javascript">var MODX_MEDIA_PATH = "media";</script>
                  <script type="text/javascript">var MODX_MEDIA_PATH = "media";</script>
                  


                  Been getting this too with the Mint Stats plugin I’m working on too. Hmm...what on earth is causing this? Plus, it looks like the plugin doesn’t get processed immediately when a page is accessed for the first time before it’s actually cached. Weird!
                    Jeff Whitfield

                    "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."
                    • 32241
                    • 1,495 Posts
                    I think we can use
                    $modx->regClientStartupScript('Any string in here', true);
                    to avoid using preg_replace function, which is it’s built in into MODx as well.

                    One thing to note though, the API need to be fixed first.
                      Wendy Novianto
                      [font=Verdana]PT DJAMOER Technology Media
                      [font=Verdana]Xituz Media
                      • 4018
                      • 1,131 Posts
                      Ah...clever! That might just do the trick! Will test and let everyone know! laugh
                        Jeff Whitfield

                        "I like my coffee hot and strong, like I like my women, hot and strong... with a spoon in them."