GoogleAnalytics.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ;(function() {
  2. var firstVisitTime = null;
  3. var previousVisitTime = null;
  4. var currentvisitTime = null;
  5. var visitsCount = null;
  6. var visitorId = null;
  7. var widgetName = null;
  8. var uniqueId = null;
  9. var hostname = null;
  10. var page_encoding = null;
  11. //encoding and hostname are optional parameters - used in pageview tracking
  12. GoogleAnalytics = function(_widgetName, _uniqueId, _hostname, _encoding)
  13. {
  14. widgetName = _widgetName;
  15. uniqueId = _uniqueId;
  16. if(_hostname == undefined || _hostname == null)
  17. {
  18. hostname = '127.0.0.1'; //assume default is localhost
  19. }
  20. else
  21. {
  22. hostname = _hostname;
  23. }
  24. if(_encoding == undefined || _encoding == null)
  25. {
  26. page_encoding = 'UTF-8'; //assume default is UTF-8
  27. }
  28. else
  29. {
  30. page_encoding = _encoding;
  31. }
  32. initialize();
  33. }
  34. GoogleAnalytics.prototype.trackEvent = function(category, action, label)
  35. {
  36. if(label == undefined || label == null)
  37. {
  38. label = '';
  39. }
  40. httpReq(
  41. getTrackURL(category, action, label),
  42. trackEventHandler
  43. );
  44. }
  45. GoogleAnalytics.prototype.trackPageview = function(a_URLToTrack)
  46. {
  47. if(a_URLToTrack == undefined || a_URLToTrack == null)
  48. {
  49. a_URLToTrack = '/'; //assume default is root
  50. }
  51. httpReq(
  52. getTrackURLPageView(a_URLToTrack),
  53. trackPageviewHandler
  54. );
  55. }
  56. function saveData()
  57. {
  58. var toSave = firstVisitTime + "|" +
  59. currentVisitTime + "|" +
  60. visitorId + "|" +
  61. visitsCount;
  62. widget.setPreferenceForKey(toSave, 'UserTracking');
  63. }
  64. function initialize()
  65. {
  66. var savedData = widget.preferenceForKey('UserTracking');
  67. var now = Math.floor(new Date().getTime() / 1000);
  68. currentVisitTime = now;
  69. if(savedData)
  70. {
  71. var splitted = savedData.split('|');
  72. firstVisitTime = splitted[0];
  73. previousVisitTime = splitted[1];
  74. visitorId = splitted[2];
  75. visitsCount = parseInt(splitted[3]) + 1;
  76. }
  77. else
  78. {
  79. firstVisitTime = now;
  80. previousVisitTime = now;
  81. visitorId = Math.floor(Math.random() * 1000000000);
  82. visitsCount = 1;
  83. }
  84. saveData();
  85. }
  86. function trackEventHandler(req, success)
  87. {
  88. alert(1);
  89. }
  90. function trackPageviewHandler(req, success)
  91. {
  92. alert(2);
  93. }
  94. function getTrackURL(category, action, label)
  95. {
  96. return "http://www.google-analytics.com/__utm.gif?" +
  97. "utmwv=4.6.5&" +
  98. "utmn=" + Math.floor(Math.random() * 100000000) + "&" +
  99. "utmhn=127.0.0.1&" +
  100. "utmt=event&" +
  101. "utme=5(" + encodeURIComponent(category) +
  102. "*" + encodeURIComponent(action) +
  103. "*" + encodeURIComponent(label) + ")&" +
  104. "utmcn=1&" +
  105. "utmdt=" + encodeURIComponent(widgetName) + "&" +
  106. "utmhid=" + Math.floor(Math.random() * 100000000) + "&" +
  107. "utmr=0&" +
  108. "utmp=%2Findex.html&" +
  109. "utmac=" + uniqueId + "&" +
  110. "utmcc=__utma%3D1." + visitorId +
  111. "." + firstVisitTime +
  112. "." + previousVisitTime + "." + currentVisitTime +
  113. "." + visitsCount +
  114. "%3B%2B__utmz%3D1." + firstVisitTime +
  115. "." + visitsCount +
  116. ".1.utmcsr%3D(direct)|utmccn%3D(direct)|utmcmd%3D(none)%3B";
  117. }
  118. function getTrackURLPageView(a_URLToTrack)
  119. {
  120. return "http://www.google-analytics.com/__utm.gif?" +
  121. "utmwv=4.8.6" +
  122. "utmn=" + Math.floor(Math.random() * 100000000) + "&" +
  123. "utmhn=" + hostname + "&" +
  124. "utmcs=" + page_encoding +
  125. "utmdt=" + encodeURIComponent(widgetName) + "&" +
  126. "utmhid=" + Math.floor(Math.random() * 100000000) + "&" +
  127. "utmr=-&" +
  128. "utmp=" + encodeURIComponent(a_URLToTrack) + "&" +
  129. "utmac=" + uniqueId + "&" +
  130. "utmcc=__utma%3D1." + visitorId +
  131. "." + firstVisitTime +
  132. "." + previousVisitTime + "." + currentVisitTime +
  133. "." + visitsCount +
  134. "%3B%2B__utmz%3D1." + firstVisitTime +
  135. "." + visitsCount +
  136. ".1.utmcsr%3D(direct)|utmccn%3D(direct)|utmcmd%3D(none)%3B&utmu=D";
  137. }
  138. function httpReq(url, func)
  139. {
  140. try
  141. {
  142. netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
  143. }
  144. catch(e)
  145. {
  146. }
  147. var request = new XMLHttpRequest();
  148. request.onreadystatechange = function()
  149. {
  150. if(request.readyState == 4)
  151. {
  152. if(request.status == 200)
  153. func(request, true);
  154. else
  155. func(request, false);
  156. }
  157. }
  158. request.open( "GET", url, true );
  159. request.send(null);
  160. }
  161. })();