Mod_pagespeed不适用于make_google_analytics_async

时间:2015-06-13 10:48:17

标签: .htaccess google-analytics mod-pagespeed

这是我在.htaccess文件中的代码...我确信mode_pagespeed有效。

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

在html中我添加了一个java脚本代码(我得到了它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>

我错了什么?这不起作用,但对于另一个过滤器insert_ga有效,但未进行优化。

1 个答案:

答案 0 :(得分:0)

您需要使用函数替换GLUE_SCRIPT。来自Docs

  

其中GLUE_SCRIPT是定义_modpagespeed_getRewriteTracker函数的JavaScript,用于返回将同步API的所有方法映射到异步API的对象。

如果您按照提供的链接下方的example进行操作,则可以看到完整的实施方式。

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) {
}