/pokkies/PokkiAtom/js/lib/GAPokki.js

https://github.com/CambriaK/Pokki-1 · JavaScript · 230 lines · 177 code · 31 blank · 22 comment · 34 complexity · b69b9c164fab124d6a9bfdcf6084226e MD5 · raw file

  1. // Google Analytics Wrapper for Pokki
  2. // Example usage
  3. //ga_pokki._setAccount('--GA-ACCOUNT-ID--');
  4. //ga_pokki._setDomain('--YOUR-DOMAIN--');
  5. //ga_pokki._trackPageview('/index', 'optional title');
  6. //ga_pokki._trackEvent('category', 'action', 'label', 'value');
  7. (function() {
  8. var VERSION = '1.1';
  9. var IS_DEBUG = false;
  10. var LocalStorage = function(key, initial_value) {
  11. if (window.localStorage.getItem(key) == null && initial_value != null) {
  12. window.localStorage.setItem(key, initial_value);
  13. }
  14. this._get = function() {
  15. return window.localStorage.getItem(key);
  16. };
  17. this._set = function(value) {
  18. return window.localStorage.setItem(key, value);
  19. };
  20. this._remove = function() {
  21. return window.localStorage.removeItem(key);
  22. };
  23. this.toString = function() {
  24. return this._get();
  25. };
  26. };
  27. ga_pokki = new function() {
  28. var that = this;
  29. var initialized = false;
  30. var tracking_code_url = 'http://www.google-analytics.com/ga.js';
  31. var beacon_url = 'http://www.google-analytics.com/__utm.gif';
  32. var utmac = false; // set by calling _setAccount
  33. var utmhn = false; // set by calling _setDomain
  34. var utmwv = '4.3'; // tracking api version
  35. var utmcs = 'UTF-8'; // charset
  36. var utmul = 'en-us'; // language
  37. var utmdt = '-'; // page title
  38. var utmn = 0; // random number
  39. var utmt = 'event'; // analytics type
  40. var utmhid = 0; // unique id per session
  41. var uid = new LocalStorage('ga_pokki_uid');
  42. var uid_rand = new LocalStorage('ga_pokki_uid_rand');
  43. var session_cnt = new LocalStorage('ga_pokki_session_cnt');
  44. var f_session = new LocalStorage('ga_pokki_f_session');
  45. var l_session = new LocalStorage('ga_pokki_l_session');
  46. var first_run = new LocalStorage('ga_pokki_first_run');
  47. var c_session = 0;
  48. function rand(min, max) {
  49. return min + Math.floor(Math.random() * (max - min));
  50. }
  51. function get_random() {
  52. return rand(100000000,999999999);
  53. }
  54. function return_cookies() {
  55. // utma represents user, should exist for lifetime: [user_id].[random #].[first session timestamp].[last session timestamp].[start of this session timestamp].[total # of sessions]
  56. // utmb is a session, [user_id].[requests_per_session?].[??].[start of session timestamp]
  57. // utmc is a session, [user_id]
  58. // utmz is a referrer cookie
  59. var cookie = uid._get();
  60. var ret = '__utma=' + cookie + '.' + uid_rand._get() + '.' + f_session._get() + '.' + l_session._get() + '.' + c_session + '.' + session_cnt._get() + ';';
  61. ret += '+__utmz=' + cookie + '.' + c_session + '.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none);';
  62. ret += '+__utmc=' + cookie + ';';
  63. ret += '+__utmb=' + cookie + '.' + request_cnt + '.10.' + c_session + ';';
  64. return ret;
  65. }
  66. function generate_query_string(params) {
  67. var qa = [];
  68. for (var key in params) {
  69. qa.push(key + '=' + encodeURIComponent(params[key]));
  70. }
  71. return '?' + qa.join('&');
  72. }
  73. function gainit() {
  74. c_session = (new Date()).getTime();
  75. if(IS_DEBUG) console.log('gainit', c_session);
  76. request_cnt = 0;
  77. utmhid = get_random();
  78. if (uid._get() == null) {
  79. uid._set(rand(10000000,99999999));
  80. uid_rand._set(rand(1000000000,2147483647));
  81. }
  82. if (session_cnt._get() == null) {
  83. session_cnt._set(1);
  84. }
  85. else {
  86. session_cnt._set(parseInt(session_cnt._get()) + 1);
  87. }
  88. if (f_session._get() == null) {
  89. f_session._set(c_session);
  90. }
  91. if (l_session._get() == null) {
  92. l_session._set(c_session);
  93. }
  94. // event to reset session when popup closes
  95. pokki.addEventListener('popup_hidden', function() {
  96. // user has specified both required parameters...
  97. if(utmac && utmhn) {
  98. that._trackEvent('User', 'PopupHidden');
  99. }
  100. if(IS_DEBUG) console.log('resetting session');
  101. l_session._set(c_session);
  102. request_cnt = 0;
  103. utmhid = get_random();
  104. });
  105. // event to start session when popup opens
  106. pokki.addEventListener('popup_showing', function() {
  107. // don't run the first time
  108. if(initialized) {
  109. c_session = (new Date()).getTime();
  110. session_cnt._set(parseInt(session_cnt._get()) + 1);
  111. if(IS_DEBUG) console.log('new current session time', c_session);
  112. }
  113. else {
  114. initialized = true;
  115. }
  116. });
  117. // event to log an icon click and first run
  118. pokki.addEventListener('popup_shown', function() {
  119. // user has specified both required parameters...
  120. if(utmac && utmhn) {
  121. // track first run
  122. if(first_run._get() == null) {
  123. that._trackEvent('User', 'FirstRun');
  124. first_run._set(1);
  125. }
  126. // track icon click
  127. that._trackEvent('User', 'IconClick');
  128. }
  129. });
  130. }
  131. // public
  132. this._setAccount = function(account_id) {
  133. if(IS_DEBUG) console.log(account_id);
  134. utmac = account_id;
  135. gainit();
  136. };
  137. // public
  138. this._setDomain = function(domain) {
  139. if(IS_DEBUG) console.log(domain);
  140. utmhn = domain;
  141. };
  142. // public
  143. this._trackPageview = function(path, title) {
  144. if(IS_DEBUG) console.log('Track Page View', arguments);
  145. request_cnt++;
  146. if (!path) {
  147. path = '/';
  148. }
  149. if(!title) {
  150. title = utmdt;
  151. }
  152. var params = {
  153. utmwv: utmwv,
  154. utmn: get_random(),
  155. utmhn: utmhn,
  156. utmcs: utmcs,
  157. utmul: utmul,
  158. utmdt: title,
  159. utmhid: utmhid,
  160. utmp: path,
  161. utmac: utmac,
  162. utmcc: return_cookies()
  163. };
  164. var url = beacon_url + generate_query_string(params);
  165. var img = new Image();
  166. img.src = url;
  167. };
  168. // public
  169. this._trackEvent = function(category, action, label, value) {
  170. if(IS_DEBUG) console.log('Track Event', arguments);
  171. request_cnt++;
  172. var event = '5(' + category + '*' + action;
  173. if (label) {
  174. event += '*' + label + ')';
  175. }
  176. else {
  177. event += ')';
  178. }
  179. if (value) {
  180. event += '(' + value + ')'
  181. }
  182. var params = {
  183. utmwv: utmwv,
  184. utmn: get_random(),
  185. utmhn: utmhn,
  186. utmcs: utmcs,
  187. utmul: utmul,
  188. utmt: utmt,
  189. utme: event,
  190. utmhid: utmhid,
  191. utmac: utmac,
  192. utmcc: return_cookies()
  193. };
  194. var url = beacon_url + generate_query_string(params);
  195. var img = new Image();
  196. img.src = url;
  197. };
  198. };
  199. })();