]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
use async, domain-aware GoogleAnalytics JS
authorEvan Prodromou <evan@status.net>
Mon, 20 Jun 2011 20:38:00 +0000 (16:38 -0400)
committerEvan Prodromou <evan@status.net>
Mon, 20 Jun 2011 20:38:00 +0000 (16:38 -0400)
plugins/GoogleAnalytics/GoogleAnalyticsPlugin.php

index b34fe203ec9075f2ec54d3a87d2f93094152e6bc..4e0bdf75362c6a99b5dfcb034f2f8a9138c0e43a 100644 (file)
@@ -49,31 +49,57 @@ if (!defined('STATUSNET')) {
  */
 class GoogleAnalyticsPlugin extends Plugin
 {
-    var $code = null;
+    const VERSION = '0.2';
 
     function __construct($code=null)
     {
-        $this->code = $code;
+        if (!empty($code)) {
+            global $config;
+            $config['googleanalytics']['code'] = $code;
+        }
+
         parent::__construct();
     }
 
     function onEndShowScripts($action)
     {
-        $js1 = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");'.
-          'document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));';
-        $js2 = sprintf('try{'.
-                       'var pageTracker = _gat._getTracker("%s");'.
-                       'pageTracker._trackPageview();'.
-                       '} catch(err) {}',
-                       $this->code);
-        $action->inlineScript($js1);
-        $action->inlineScript($js2);
+        $code = common_config('googleanalytics', 'code');
+        $domain = common_config('googleanalytics', 'domain');
+
+        $js = <<<ENDOFSCRIPT0
+
+var _gaq = _gaq || [];
+_gaq.push(['_setAccount', '{$code}']);
+_gaq.push(['_trackPageview']);
+
+ENDOFSCRIPT0;
+
+if (!empty($domain)) {
+        $js .= <<<ENDOFSCRIPT1
+
+_gaq.push(['_setDomainName', '{$domain}']);
+_gaq.push(['_setAllowHash', false]);
+
+ENDOFSCRIPT1;
+}
+
+        $js .= <<<ENDOFSCRIPT2
+
+(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);
+})();
+
+ENDOFSCRIPT2;
+
+       $action->inlineScript($js);
     }
 
     function onPluginVersion(&$versions)
     {
         $versions[] = array('name' => 'GoogleAnalytics',
-                            'version' => STATUSNET_VERSION,
+                            'version' => self::VERSION,
                             'author' => 'Evan Prodromou',
                             'homepage' => 'http://status.net/wiki/Plugin:GoogleAnalytics',
                             'rawdescription' =>