diff options
author | BeezyT <timo@ezdesign.de> | 2012-10-17 09:12:17 +0400 |
---|---|---|
committer | BeezyT <timo@ezdesign.de> | 2012-10-17 09:12:17 +0400 |
commit | bc5ff08cebcb49210b77095a08c121cd39a920ef (patch) | |
tree | b9f298e809d408579b79e5524d18aa249c046ef8 /js | |
parent | 400e5bf353197b74762c13fdb94922db3a890f14 (diff) |
refs #2465: Starting the Insight plugin (in-site analytics)
This is the code I wrote about a year ago :)
There are several things that will be changed / removed but it's still good to have it in SVN - that's why I commit it like this.
git-svn-id: http://dev.piwik.org/svn/trunk@7223 59fd770c-687e-43c8-a1e3-f5a4ff64c105
Diffstat (limited to 'js')
-rw-r--r-- | js/piwik.js | 97 |
1 files changed, 94 insertions, 3 deletions
diff --git a/js/piwik.js b/js/piwik.js index ae0961cef5..8b6ace3ab0 100644 --- a/js/piwik.js +++ b/js/piwik.js @@ -897,6 +897,90 @@ var } return title; } + + + /************************************************************ + * Page Insight + ************************************************************/ + + /* + * check whether this is an insight session + */ + function isInsightSession(configTrackerUrl, configTrackerSiteId) { + var windowName = 'Piwik_Insight'; + var referrer = documentAlias.referrer; + var testReferrer = configTrackerUrl; + // remove piwik.php from referrer + testReferrer = testReferrer.substring(0, testReferrer.length - 9); + // remove protocol + if (testReferrer.substring(0, 7) == 'http://') { + testReferrer = testReferrer.substring(7, testReferrer.length); + } else { + testReferrer = testReferrer.substring(8, testReferrer.length); + } + // build referrer regex + var referrerRegExp = new RegExp('^(http|https)://' + testReferrer + + 'index\\.php\\?module=Insight&action=startInsightSession' + + '&idsite=([0-9]+)&period=([^&]+)&date=([^&]+)&urls=([^&]+)$'); + + var match; + if (match = referrer.match(referrerRegExp)) { + // check idsite + var idsite = match[2]; + if (parseInt(idsite, 10) !== configTrackerSiteId) { + return false; + } + // store insight session info in window name + var period = match[3]; + var date = match[4]; + var urls = unescape(match[5]); + window.name = windowName + '###' + period + '###' + date + '###' + urls; + } + + // retrieve and check data from window name + var windowNameParts = windowAlias.name.split('###'); + return windowNameParts.length == 4 && windowNameParts[0] == windowName; + } + + /* + * inject the script needed for insight + */ + function injectInsightScripts(configTrackerUrl, configTrackerSiteId) { + var windowNameParts = window.name.split('###'); + var root = configTrackerUrl.substring(0, configTrackerUrl.length - 9); // remove piwik.php + var period = windowNameParts[1]; + var date = windowNameParts[2]; + var urls = windowNameParts[3].split('##'); + + var loaded = false; + var onLoad = function() { + if (!loaded) { + loaded = true; + Piwik_Insight_Client.initialize(root, configTrackerSiteId, + period, date, urls); + } + }; + + var script = document.createElement('script'); + script.type = 'text/javascript'; + + script.onreadystatechange = function () { + if (this.readyState == 'loaded' || this.readyState == 'complete') { + onLoad(); + } + }; + script.onload = onLoad; + + script.src = root + 'plugins/Insight/client/client.js'; + + var head = document.getElementsByTagName('head')[0]; + head.appendChild(script); + } + + /************************************************************ + * End Piwik Insight + ************************************************************/ + /* * Piwik Tracker class @@ -2574,9 +2658,16 @@ var * @param mixed customData */ trackPageView: function (customTitle, customData) { - trackCallback(function () { - logPageView(customTitle, customData); - }); + if (isInsightSession(configTrackerUrl, configTrackerSiteId)) { + trackCallback(function () { + injectInsightScripts(configTrackerUrl, configTrackerSiteId); + }); + } + else { + trackCallback(function () { + logPageView(customTitle, customData); + }); + } }, /** |