Danny Ng\'s script for accessing GA Cookies


/ Published in: JavaScript
Save to your folder(s)

for accessing all the values in the GA cookies. I\\\\\\\'ve used this to track a session ID into Kiss Insights and also into GA on a custom variable so that Kiss Insights responses can be brought in against GA data.


Copy this code and paste it in your HTML
  1. /**
  2.  * @author: Danny Ng (http://www.dannytalk.com/2010/08/19/read-google-an�¢ï¿½�¦-cookie-script/)
  3.  * @modified: 19/08/10
  4.  * @notes: Free to use and distribute without altering this comment. Would appreciate a link back :)
  5.  */
  6.  
  7. // Strip leading and trailing white-space
  8. String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g, ''); }
  9.  
  10. // Check if string is empty
  11. String.prototype.empty = function() {
  12. if (this.length == 0)
  13. return true;
  14. else if (this.length > 0)
  15. return /^\s*$/.test(this);
  16. }
  17.  
  18. // Breaks cookie into an object of keypair cookie values
  19. function crumbleCookie(c)
  20. {
  21. var cookie_array = document.cookie.split(';');
  22. var keyvaluepair = {};
  23. for (var cookie = 0; cookie < cookie_array.length; cookie++)
  24. {
  25. var key = cookie_array[cookie].substring(0, cookie_array[cookie].indexOf('=')).trim();
  26. var value = cookie_array[cookie].substring(cookie_array[cookie].indexOf('=')+1, cookie_array[cookie].length).trim();
  27. keyvaluepair[key] = value;
  28. }
  29.  
  30. if (c)
  31. return keyvaluepair[c] ? keyvaluepair[c] : null;
  32.  
  33. return keyvaluepair;
  34. }
  35.  
  36. /**
  37.  * For GA cookie explanation, see http://services.google.com/analytics/breeze/en/ga_cookies/index.html.
  38.  *
  39.  * @return - <void>
  40.  *
  41.  * @pre-condition - pageTracker initialised properly
  42.  * @post-condition - provides 'get' methods to access specific values in the Google Analytics cookies
  43.  */
  44. function gaCookies()
  45. {
  46. // Cookie syntax: domain-hash.unique-id.ftime.ltime.stime.session-counter
  47. var utma = function() {
  48. var utma_array;
  49.  
  50. if (crumbleCookie('__utma'))
  51. utma_array = crumbleCookie('__utma').split('.');
  52. else
  53. return null;
  54.  
  55. var domainhash = utma_array[0];
  56. var uniqueid = utma_array[1];
  57. var ftime = utma_array[2];
  58. var ltime = utma_array[3];
  59. var stime = utma_array[4];
  60. var sessions = utma_array[5];
  61.  
  62. return {
  63. 'cookie': utma_array,
  64. 'domainhash': domainhash,
  65. 'uniqueid': uniqueid,
  66. 'ftime': ftime,
  67. 'ltime': ltime,
  68. 'stime': stime,
  69. 'sessions': sessions
  70. };
  71. };
  72.  
  73. // Cookie syntax: domain-hash.gif-requests.10.stime
  74. var utmb = function() {
  75. var utmb_array;
  76.  
  77. if (crumbleCookie('__utmb'))
  78. utmb_array = crumbleCookie('__utmb').split('.');
  79. else
  80. return null;
  81. var gifrequest = utmb_array[1];
  82.  
  83. return {
  84. 'cookie': utmb_array,
  85. 'gifrequest': gifrequest
  86. };
  87. };
  88.  
  89. // Cookie syntax: domain-hash.value
  90. var utmv = function() {
  91. var utmv_array;
  92.  
  93. if (crumbleCookie('__utmv'))
  94. utmv_array = crumbleCookie('__utmv').split('.');
  95. else
  96. return null;
  97.  
  98. var value = utmv_array[1];
  99.  
  100. return {
  101. 'cookie': utmv_array,
  102. 'value': value
  103. };
  104. };
  105.  
  106. // Cookie syntax: domain-hash.ftime.?.?.utmcsr=X|utmccn=X|utmcmd=X|utmctr=X
  107. var utmz = function() {
  108. var utmz_array, source, medium, name, term, content, gclid;
  109.  
  110. if (crumbleCookie('__utmz'))
  111. utmz_array = crumbleCookie('__utmz').split('.');
  112. else
  113. return null;
  114.  
  115. var utms = utmz_array[4].split('|');
  116. for (var i = 0; i < utms.length; i++) {
  117. var key = utms[i].substring(0, utms[i].indexOf('='));
  118. var val = decodeURIComponent(utms[i].substring(utms[i].indexOf('=')+1, utms[i].length));
  119. val = val.replace(/^\(|\)$/g, ''); // strip () brackets
  120. switch(key)
  121. {
  122. case 'utmcsr':
  123. source = val;
  124. break;
  125. case 'utmcmd':
  126. medium = val;
  127. break;
  128. case 'utmccn':
  129. name = val;
  130. break;
  131. case 'utmctr':
  132. term = val;
  133. break;
  134. case 'utmcct':
  135. content = val;
  136. break;
  137. case 'utmgclid':
  138. gclid = val;
  139. break;
  140. }
  141. }
  142.  
  143. return {
  144. 'cookie': utmz_array,
  145. 'source': source,
  146. 'medium': medium,
  147. 'name': name,
  148. 'term': term,
  149. 'content': content,
  150. 'gclid': gclid
  151. };
  152. };
  153.  
  154. // Establish public methods
  155.  
  156. // utma cookies
  157. this.getDomainHash = function() { return (utma() && utma().domainhash) ? utma().domainhash : null };
  158. this.getUniqueId = function() { return (utma() && utma().uniqueid) ? utma().uniqueid : null };
  159.  
  160. this.getInitialVisitTime = function() { return (utma() && utma().ftime) ? utma().ftime : null };
  161. this.getPreviousVisitTime = function() { return (utma() && utma().ltime) ? utma().ltime : null };
  162. this.getCurrentVisitTime = function() { return (utma() && utma().stime) ? utma().stime : null };
  163. this.getSessionCounter = function() { return (utma() && utma().sessions) ? utma().sessions : null };
  164.  
  165. // utmb cookies
  166. this.getGifRequests = function() { return (utmb() && utmb().gifrequest) ? utmb().gifrequest : null };
  167.  
  168. // utmv cookies
  169. this.getUserDefinedValue = function () { return (utmv() && utmv().value) ? decodeURIComponent(utmv().value) : null };
  170.  
  171. // utmz cookies
  172. this.getCampaignSource = function () { return (utmz() && utmz().source) ? utmz().source : null };
  173. this.getCampaignMedium = function () { return (utmz() && utmz().medium) ? utmz().medium : null };
  174. this.getCampaignName = function () { return (utmz() && utmz().name) ? utmz().name : null };
  175. this.getCampaignTerm = function () { return (utmz() && utmz().term) ? utmz().term : null};
  176. this.getCampaignContent = function () { return (utmz() && utmz().content) ? utmz().content : null };
  177. this.getGclid = function () { return (utmz() && utmz().gclid) ? utmz().gclid : null };
  178. }

URL: http://www.dannytalk.com/2010/08/19/read-google-analytics-cookie-script/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.