Mod_pagespeed do not work for make_google_analytics_async

419 views Asked by At

this is my code in .htaccess file ... and I am sure mode_pagespeed works.

<IfModule pagespeed_module>
ModPagespeed on
ModPagespeedEnableFilters make_google_analytics_async
.
.
.
</IfModule>

and in html I add a java script code (I got it here)

<html>
<head>

<script type='text/javascript'>
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  GLUE_SCRIPT
  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>
<script type="text/javascript">
  try {
    var pageTracker = _modpagespeed_getRewriteTracker("UA-63697801-1");
    pageTracker._trackPageview();
  } catch(err) {}
</script>
</head>
<body>

</body>
</html>

what's my wrong? this is not work but for another filter insert_ga works but that is not optimized.

1

There are 1 answers

0
Raja Khoury On

You need to replace GLUE_SCRIPT with a function. From the Docs

where GLUE_SCRIPT is JavaScript that defines the _modpagespeed_getRewriteTracker function to return an object that maps all the methods of the synchronous API to the asynchronous API.

if you follow the example below the link provided, you can see a full implementation.

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
var _gaq = _gaq || [];
(function () {
    function functionName(fn) {
        var name = /\W*function\s+([\w\$]+)\(/.exec(fn);
        if (!name)
            return 'No name';
        return name[1];
    }

    var nameSpace = '_gat';
    var existingGat = window[nameSpace];
    if (existingGat && typeof existingGat['_getTracker'] == 'function') {
        return;
    }

    var gaqAccounts = [];

    function setAccount(acct, prefix) {
        if (gaqAccounts[prefix] != acct) {
            gaqAccounts[prefix] = acct;
            _gaq.push([prefix + '_setAccount', acct]);
        }
    }

    window['_modpagespeed_getRewriteTracker'] = function (tracker_acct,
                                                          tracker_name) {
        var prefix = tracker_name ? tracker_name + '.' : '';

        function deferTrackerFunc(fn) {
            return function () {
                setAccount(tracker_acct, prefix);
                var pushArgs = [fn];
                [].push.apply(pushArgs, arguments);
                _gaq.push(pushArgs);
            };
        }

        var pageTrackerMethodNames = [
            '_trackPageview',
            '_trackEvent',
            '_trackTrans',
            '_addIgnoredOrganic',
            '_addIgnoredRef',
            '_addItem',
            '_addOrganic',
            '_addTrans',
            '_clearIgnoredOrganic',
            '_clearIgnoredRef',
            '_clearOrganic',
            '_clearXKey',
            '_clearXValue',
            '_cookiePathCopy',
            '_deleteCustomVar',
            '_link',
            '_linkByPost',
            '_sendXEvent',
            '_setAllowAnchor',
            '_setAllowHash',
            '_setAllowLinker',
            '_setAutoTrackOutbound',
            '_setCampCIdKey',
            '_setCampContentKey',
            '_setCampIdKey',
            '_setCampMediumKey',
            '_setCampNOKey',
            '_setCampNameKey',
            '_setCampSourceKey',
            '_setCampTermKey',
            '_setCampaignCookieTimeout',
            '_setCampaignTrack',
            '_setClientInfo',
            '_setCookiePath',
            '_setCookiePersistence',
            '_setCookieTimeout',
            '_setCustomVar',
            '_setDetectFlash',
            '_setDetectTitle',
            '_setDomainName',
            '_setHrefExamineLimit',
            '_setLocalGifPath',
            '_setLocalRemoteServerMode',
            '_setLocalServerMode',
            '_setMaxCustomVariables',
            '_setNamespace',
            '_setReferrerOverride',
            '_setRemoteServerMode',
            '_setSampleRate',
            '_setSessionCookieTimeout',
            '_setSessionTimeout',
            '_setTrackOutboundSubdomains',
            '_setTrans',
            '_setTransactionDelim',
            '_setVar',
            '_setVisitorCookieTimeout',
            '_setXKey',
            '_setXValue'
        ];
        var pageTracker = {
            _initData: function () {
            },
        };
        for (var i = pageTrackerMethodNames.length; i--;) {
            var n = pageTrackerMethodNames[i];
            pageTracker[n] = deferTrackerFunc(prefix + n);
        }
        return pageTracker;
    };

    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);
})();
try {
    var pageTracker = _modpagespeed_getRewriteTracker("UA-xxxx-9");
    pageTracker._trackPageview();
} catch (err) {
}