We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25663 MODX Staff
    • 12,272 Posts
    This is an auto-generated support/comment thread for Google Analytics.

    Use this forum to post any comments about this addition or any questions you have regarding its use.

    Brief Description:
    Adds a Google Analytics tag to every HTML page of your website.
      Ryan Thrash, MODX Co-Founder
      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • 32288
      • 5 Posts
      Nice one. So simple you’d think nothing was happening. It installed smoothly and I’ll now wait the ~24 hours that google takes to show results.

      I wasn’t able to rate this on the download page, for some reason. But 5 stars.
        • 22221
        • 283 Posts
        I remember of a google Analytics snippet but it isn’t in the repositoy.
        If I want to tune the code in my HTLM page (like group stat following the user group), I think it will be more easy with a snippet than a plugin ?

          • 25663 MODX Staff
          • 12,272 Posts
          Indeed it will OncleBen31. A snippet would do just what the plugin does for the most part, only not really need it to replace the closing </body> tag. Should be able to easily port the plugin to a snippet.
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 22221
            • 283 Posts
            I made a mod based on the 1.1 version.
            I add the possibility to add a user variable readable by Google Analytics which allow to segment logged in user or to filter users logged in manager. With this plugin, just add a filter to delete you developpement traffic to you statistics or consult the "All Reports" -> "Merketing optimisation" -> "Visitor Segment Performance" -> "User-defined" page to see the segmentation.

            // <?php
            // Google Analytics Plugin by Mark Kaplan moded by Oncleben31
            // 15-Oct-2006
            // version 1.1+ (based on 1.1)
            //
            // Adds your Google Analytics to every page in your site
            // The mod allows to add a variable to filter and/or segment visitor according there are logged in,
            // manager user or simple users. Essential to optimize you logs or to filter the developpement traffic on you site.
            //
            // Parameter: &account=Account;string;UA-000000-0 &filterManager= Filter Manager User;list;Yes,No;No &userGroupSegment=User Groups segmenting;list;Yes,No;No
            // Event: OnWebPagePrerender
            
            $e = $modx->Event;
            $account = isset($account)? $account: '';
            $filterManagerFlag = isset($filterManager)? (($filterManager=='Yes')? 1: 0): 0;
            $userGroupSegmentFlag = isset($userGroupSegment)? (($userGroupSegment=='Yes')? 1: 0): 0;
            $userName = $modx->getLoginUserName();
            
            
            $script = $account? '
            <script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
            </script>
            <script type="text/javascript">
            _uacct = "'.$account.'";
            urchinTracker();
            </script>
            ': '
            
            <!-- Google Analytics Account Not Supplied -->
            
            ';
            
            switch ($e->name) {
            case "OnWebPagePrerender":
            $googleize = ($modx->documentObject['donthit']==0 && $modx->documentObject['contentType']=='text/html');
            if ($googleize) {
            $modx->documentOutput = preg_replace("/(<\/body>)/i", $script."\n\\1", $modx->documentOutput);
            
            // Filter users logged in Manager
            if($filterManagerFlag && isset($_SESSION['mgrValidated']) && $_SESSION['mgrValidated']) {
            $bodyScript = 'onLoad="javascript:__utmSetVar(\'admin_user\')"';
            $modx->documentOutput = preg_replace("/(<body)/i", "\\1 ".$bodyScript, $modx->documentOutput);
            }
            // Filter users logged in Manager
            elseif($userGroupSegmentFlag && $userName) {
            $bodyScript = 'onLoad="javascript:__utmSetVar(\'logged_user\')"';
            $modx->documentOutput = preg_replace("/(<body)/i", "\\1 ".$bodyScript, $modx->documentOutput);
            }
            }
            break;
            
            default :
            return; // stop here - this is very important.
            break;
            }
            // ?>


            Can you check the code because I’m not a master in PHP and in RegExp. I’m not sur to cover all the possibility with the modification of the <body> tag.
              • 31227
              • 198 Posts
              Guys, I have 2 sites/2 domains/2 google analytics on my site. Is there anyway to make this script read particular domain/templates? Thanks
                Pure MODx sites
                Viriko.ru & Viriko.com - Multilingual and running on 1 MODx
                • 25663 MODX Staff
                • 12,272 Posts
                You’d add a configuration parameter that checks the ID of the template, and then change the user account based on the result.The released version 1.2 update to this that gracefully handles http and https (andfilters out logged in manager sessions entirely, but I like the visitor segmenting even better). Thanks OncleBen31 ... care to collaborate on an update to merge the two?
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 31227
                  • 198 Posts
                  glad that more ppl would like this:) Cause from the looks of modx, i am thinking of running all of the sites I have hosted on 1 server, using only 1 modx (i love this idea, of course there is a saying - "Don’t put all your eggs in one basket") and I’d like separate analytics script for each one of them.
                    Pure MODx sites
                    Viriko.ru & Viriko.com - Multilingual and running on 1 MODx
                    • 12151
                    • 115 Posts
                    hi there,

                    i have updated this plugin for the new style analytics code but it also still supports the legacy code:

                    a new plugin parameter $gaVersion which defaults to "new". set it to "legacy" for the old style

                    see attached for updated plugin

                    cheers! smiley

                    EDIT - This update has moved to the repository - http://www.modxcms.com/Google-Analytics-1.3-1739.html
                      • 25663 MODX Staff
                      • 12,272 Posts
                      Thanks for the update highlander. Care to put that into the MODx Resources Respository?
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me