Added code for properly tracking cross-domain links, resolves #43568
authorfrancois <francois@735d13b6-9817-0410-8766-e36946ffe9aa>
Tue, 4 Dec 2012 08:50:52 +0000 (08:50 +0000)
committerfrancois <francois@735d13b6-9817-0410-8766-e36946ffe9aa>
Tue, 4 Dec 2012 08:50:52 +0000 (08:50 +0000)
git-svn-id: https://svn.typo3.org/TYPO3v4/Extensions/rsgoogleanalytics/trunk@68199 735d13b6-9817-0410-8766-e36946ffe9aa

ChangeLog
class.tx_rsgoogleanalytics.php
res/templates/codeAsynchronous.js

index c0de8e2..f372258 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2012-12-03 Francois Suter  <typo3@cobweb.ch>\r
+\r
+       * Added code for properly tracking cross-domain links, resolves #43568\r
+\r
 2012-11-28 Francois Suter  <typo3@cobweb.ch>\r
 \r
        * Removed usage of deprecated method setAllowHash, resolves #43390\r
index 0e380bb..86e0590 100644 (file)
@@ -53,6 +53,11 @@ class tx_rsgoogleanalytics implements t3lib_singleton {
        protected $domainConfig = array();
 
        /**
+        * @var string
+        */
+       protected $selectedDomain;
+
+       /**
         * @var array
         */
        protected $eCommerce = array('items' => array(), 'transaction' => array());
@@ -149,23 +154,22 @@ class tx_rsgoogleanalytics implements t3lib_singleton {
                        if ($this->modConfig['multipleDomains'] && $this->modConfig['multipleDomains'] != 'false') {
                                        // Extract the list of domains
                                $this->domainConfig['multiple'] = t3lib_div::trimExplode(',', $this->modConfig['multipleDomains.']['domainNames'], TRUE);
-                               $selectedDomain = 'none';
                                $numberOfDomains = count($this->domainConfig['multiple']);
                                        // If there's only one, use it as is
                                if ($numberOfDomains == 1) {
-                                       $selectedDomain = $this->domainConfig['multiple'][0];
+                                       $this->selectedDomain = $this->domainConfig['multiple'][0];
 
                                        // If there are more than one, try to match to the current domain
                                } elseif ($numberOfDomains > 1) {
                                        $currentDomain = t3lib_div::getIndpEnv('TYPO3_HOST_ONLY');
                                        for ($i = 0; $i < $numberOfDomains; $i++) {
                                                if (strstr($currentDomain, $this->domainConfig['multiple'][$i]) == $this->domainConfig['multiple'][$i]) {
-                                                       $selectedDomain = $this->domainConfig['multiple'][$i];
+                                                       $this->selectedDomain = $this->domainConfig['multiple'][$i];
                                                        break;
                                                }
                                        }
                                }
-                               $this->commands[10] = $this->buildCommand('setDomainName', array($selectedDomain));
+                               $this->commands[10] = $this->buildCommand('setDomainName', array((empty($this->selectedDomain)) ? 'none' : $this->selectedDomain));
                                $this->commands[11] = $this->buildCommand('setAllowLinker', array(TRUE));
 
                        } elseif ($this->modConfig['trackSubDomains'] && $this->modConfig['trackSubDomains'] != 'false') {
@@ -366,6 +370,26 @@ class tx_rsgoogleanalytics implements t3lib_singleton {
        }
 
        /**
+        * Checks whether the given URL should be considered as being cross-domain
+        *
+        * @param string $url The URL to check
+        * @return bool
+        */
+       protected function isUrlCrossDomain($url) {
+               $urlParts = parse_url($url);
+                       // Loop on all configured domains
+               $numberOfDomains = count($this->domainConfig['multiple']);
+               for ($i = 0; $i < $numberOfDomains; $i++) {
+                       $currentDomain = $this->domainConfig['multiple'][$i];
+                               // Stop at the first domain that matches and return true
+                       if ($currentDomain != $this->selectedDomain &&  strpos($urlParts['host'], $currentDomain)) {
+                               return TRUE;
+                       }
+               }
+               return FALSE;
+       }
+
+       /**
         * This method checks whether the given file is in the paths to track
         *
         * @param string $file Filename (with directories from site root) which is linked
@@ -419,13 +443,20 @@ class tx_rsgoogleanalytics implements t3lib_singleton {
                        // Make sure the domain configuration has been handled
                $this->makeDomainConfiguration();
 
+               $url = $params['finalTagParts']['url'];
                $function = FALSE;
                switch ($params['finalTagParts']['TYPE']) {
                        case 'page':
-                               // do nothing on normal links
+                                       // If cross-domain linking is activated, check if the link is indeed cross-domain
+                                       // Otherwise, nothing special needs to be done
+                               if (!empty($this->selectedDomain)) {
+                                               // If typolink URL is cross-domain, add GA link code
+                                       if ($this->isUrlCrossDomain($url)) {
+                                               $function = 'RsGoogleAnalytics.crossDomainLink(this); return false;';
+                                       }
+                               }
                                break;
                        case 'url' :
-                               $url = $params['finalTagParts']['url'];
                                if ( /*checkInMultiple($url)*/
                                        0) {
                                        $function = $this->buildCommand('link', array($url)) . 'return false;';
@@ -435,12 +466,11 @@ class tx_rsgoogleanalytics implements t3lib_singleton {
                                break;
                        case 'file':
                                if ($this->modConfig['trackDownloads']) {
-                                       $fileName = $params['finalTagParts']['url'];
-                                       $fileInfo = pathinfo($fileName);
+                                       $fileInfo = pathinfo($url);
                                        // TODO: provide hook where downloader extension can register their transformation function
 
-                                       if ($this->modConfig['trackDownloads'] == '!ALL' || $this->checkFile($fileName)) {
-                                               $function = $this->buildCommand('trackEvent', array('Download', $fileInfo['extension'], $fileName));
+                                       if ($this->modConfig['trackDownloads'] == '!ALL' || $this->checkFile($url)) {
+                                               $function = $this->buildCommand('trackEvent', array('Download', $fileInfo['extension'], $url));
                                        }
                                }
                                break;
index d6b9c50..cf6c2d9 100644 (file)
@@ -7,3 +7,27 @@ _gaq.push(['_setAccount', "###ACCOUNT###"]);
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\r
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\r
 })();\r
+\r
+       // Provide convenience methods related to GA API usage, wrapped in some global object\r
+var RsGoogleAnalytics = {\r
+       /**\r
+        * Opens a cross-domain link while passing the appropriate parameters for continued visitor tracking\r
+        * (see https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._getLinkerUrl)\r
+        *\r
+        * @param link <a> tag that was clicked\r
+        */\r
+       crossDomainLink: function(link) {\r
+               _gaq.push(function() {\r
+                               // Get the default tracker\r
+                       var pageTracker = _gat._getTrackerByName();\r
+                               // Extract the destination URL and attach tracking information to it\r
+                       var linkerUrl = pageTracker._getLinkerUrl(link.href);\r
+                               // Redirect to cross-domain URL, optionally in new window if target is so defined\r
+                       if (link.target == '_blank') {\r
+                               window.open(linkerUrl);\r
+                       } else {\r
+                               window.location = linkerUrl;\r
+                       }\r
+               });\r
+       }\r
+};\r