123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- ;(function() {
- var firstVisitTime = null;
- var previousVisitTime = null;
- var currentvisitTime = null;
- var visitsCount = null;
- var visitorId = null;
-
- var widgetName = null;
- var uniqueId = null;
- var hostname = null;
- var page_encoding = null;
-
- //encoding and hostname are optional parameters - used in pageview tracking
- GoogleAnalytics = function(_widgetName, _uniqueId, _hostname, _encoding)
- {
- widgetName = _widgetName;
- uniqueId = _uniqueId;
-
- if(_hostname == undefined || _hostname == null)
- {
- hostname = '127.0.0.1'; //assume default is localhost
- }
- else
- {
- hostname = _hostname;
- }
- if(_encoding == undefined || _encoding == null)
- {
- page_encoding = 'UTF-8'; //assume default is UTF-8
- }
- else
- {
- page_encoding = _encoding;
- }
-
- initialize();
- }
- GoogleAnalytics.prototype.trackEvent = function(category, action, label)
- {
- if(label == undefined || label == null)
- {
- label = '';
- }
-
- httpReq(
- getTrackURL(category, action, label),
- trackEventHandler
- );
- }
- GoogleAnalytics.prototype.trackPageview = function(a_URLToTrack)
- {
- if(a_URLToTrack == undefined || a_URLToTrack == null)
- {
- a_URLToTrack = '/'; //assume default is root
- }
- httpReq(
- getTrackURLPageView(a_URLToTrack),
- trackPageviewHandler
- );
- }
-
- function saveData()
- {
- var toSave = firstVisitTime + "|" +
- currentVisitTime + "|" +
- visitorId + "|" +
- visitsCount;
-
- widget.setPreferenceForKey(toSave, 'UserTracking');
- }
- function initialize()
- {
- var savedData = widget.preferenceForKey('UserTracking');
-
- var now = Math.floor(new Date().getTime() / 1000);
-
- currentVisitTime = now;
-
- if(savedData)
- {
- var splitted = savedData.split('|');
-
- firstVisitTime = splitted[0];
- previousVisitTime = splitted[1];
-
- visitorId = splitted[2];
-
- visitsCount = parseInt(splitted[3]) + 1;
- }
- else
- {
- firstVisitTime = now;
-
- previousVisitTime = now;
-
- visitorId = Math.floor(Math.random() * 1000000000);
-
- visitsCount = 1;
- }
- saveData();
- }
- function trackEventHandler(req, success)
- {
- alert(1);
- }
- function trackPageviewHandler(req, success)
- {
- alert(2);
- }
- function getTrackURL(category, action, label)
- {
- return "http://www.google-analytics.com/__utm.gif?" +
- "utmwv=4.6.5&" +
- "utmn=" + Math.floor(Math.random() * 100000000) + "&" +
- "utmhn=127.0.0.1&" +
- "utmt=event&" +
- "utme=5(" + encodeURIComponent(category) +
- "*" + encodeURIComponent(action) +
- "*" + encodeURIComponent(label) + ")&" +
- "utmcn=1&" +
- "utmdt=" + encodeURIComponent(widgetName) + "&" +
- "utmhid=" + Math.floor(Math.random() * 100000000) + "&" +
- "utmr=0&" +
- "utmp=%2Findex.html&" +
- "utmac=" + uniqueId + "&" +
- "utmcc=__utma%3D1." + visitorId +
- "." + firstVisitTime +
- "." + previousVisitTime + "." + currentVisitTime +
- "." + visitsCount +
- "%3B%2B__utmz%3D1." + firstVisitTime +
- "." + visitsCount +
- ".1.utmcsr%3D(direct)|utmccn%3D(direct)|utmcmd%3D(none)%3B";
- }
- function getTrackURLPageView(a_URLToTrack)
- {
- return "http://www.google-analytics.com/__utm.gif?" +
- "utmwv=4.8.6" +
- "utmn=" + Math.floor(Math.random() * 100000000) + "&" +
- "utmhn=" + hostname + "&" +
- "utmcs=" + page_encoding +
- "utmdt=" + encodeURIComponent(widgetName) + "&" +
- "utmhid=" + Math.floor(Math.random() * 100000000) + "&" +
- "utmr=-&" +
- "utmp=" + encodeURIComponent(a_URLToTrack) + "&" +
- "utmac=" + uniqueId + "&" +
- "utmcc=__utma%3D1." + visitorId +
- "." + firstVisitTime +
- "." + previousVisitTime + "." + currentVisitTime +
- "." + visitsCount +
- "%3B%2B__utmz%3D1." + firstVisitTime +
- "." + visitsCount +
- ".1.utmcsr%3D(direct)|utmccn%3D(direct)|utmcmd%3D(none)%3B&utmu=D";
- }
- function httpReq(url, func)
- {
- try
- {
- netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
- }
- catch(e)
- {
- }
-
- var request = new XMLHttpRequest();
-
- request.onreadystatechange = function()
- {
- if(request.readyState == 4)
- {
- if(request.status == 200)
- func(request, true);
- else
- func(request, false);
- }
- }
- request.open( "GET", url, true );
-
- request.send(null);
- }
- })();
|