Integrating Google Analytics with a CRM


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



Copy this code and paste it in your HTML
  1. <html>
  2.  
  3. <head>
  4.  
  5. <script type="text/javascript">
  6. var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  7. document.write("<script src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'>" + "</sc" + "ript>");
  8. </script>
  9.  
  10. <script type='text/javascript'>
  11. var pageTracker = _gat._getTracker("UA-1-1");
  12. pageTracker._trackPageview();
  13.  
  14. //
  15. // This is a function that I "borrowed" from the urchin.js file.
  16. // It parses a string and returns a value. I used it to get
  17. // data from the __utmz cookie
  18. //
  19. function _uGC(l,n,s) {
  20. if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
  21. var i,i2,i3,c="-";
  22. i=l.indexOf(n);
  23. i3=n.indexOf("=")+1;
  24. if (i > -1) {
  25. i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
  26. c=l.substring((i+i3),i2);
  27. }
  28. return c;
  29. }
  30.  
  31. //
  32. // Get the __utmz cookie value. This is the cookies that
  33. // stores all campaign information.
  34. //
  35. var z = _uGC(document.cookie, '__utmz=', ';');
  36. //
  37. // The cookie has a number of name-value pairs.
  38. // Each identifies an aspect of the campaign.
  39. //
  40. // utmcsr = campaign source
  41. // utmcmd = campaign medium
  42. // utmctr = campaign term (keyword)
  43. // utmcct = campaign content
  44. // utmccn = campaign name
  45. // utmgclid = unique identifier used when AdWords auto tagging is enabled
  46. //
  47. // This is very basic code. It separates the campaign-tracking cookie
  48. // and populates a variable with each piece of campaign info.
  49. //
  50. var source = _uGC(z, 'utmcsr=', '|');
  51. var medium = _uGC(z, 'utmcmd=', '|');
  52. var term = _uGC(z, 'utmctr=', '|');
  53. var content = _uGC(z, 'utmcct=', '|');
  54. var campaign = _uGC(z, 'utmccn=', '|');
  55. var gclid = _uGC(z, 'utmgclid=', '|');
  56. //
  57. // The gclid is ONLY present when auto tagging has been enabled.
  58. // All other variables, except the term variable, will be '(not set)'.
  59. // Because the gclid is only present for Google AdWords we can
  60. // populate some other variables that would normally
  61. // be left blank.
  62. //
  63. if (gclid !="-") {
  64. source = 'google';
  65. medium = 'cpc';
  66. }
  67. // Data from the custom segmentation cookie can also be passed
  68. // back to your server via a hidden form field
  69. var csegment = _uGC(document.cookie, '__utmv=', ';');
  70. if (csegment != '-') {
  71. var csegmentex = /[1-9]*?\.(.*)/;
  72. csegment = csegment.match(csegmentex);
  73. csegment = csegment[1];
  74. } else {
  75. csegment = '(not set)';
  76. }
  77.  
  78. //
  79. // One more bonus piece of information.
  80. // We're going to extract the number of visits that the visitor
  81. // has generated. It's also stored in a cookie, the __utma cookis
  82. //
  83. var a = _uGC(document.cookie, '__utma=', ';');
  84. var aParts = a.split(".");
  85. var nVisits = aParts[5];
  86.  
  87. function populateHiddenFields(f) {
  88. f.source.value = source;
  89. f.medium.value = medium;
  90. f.term.value = term;
  91. f.content.value = content;
  92. f.campaign.value = campaign;
  93. f.segment.value = csegment;
  94. f.numVisits.value = nVisits;
  95.  
  96. alert('source='+f.source.value);
  97. alert('medium='+f.medium.value);
  98. alert('term='+f.term.value);
  99. alert('content='+f.content.value);
  100. alert('campaign='+f.campaign.value);
  101. alert('custom segment='+f.segment.value);
  102. alert('number of visits='+f.numVisits.value);
  103.  
  104. return false;
  105. }
  106. </script>
  107. </head>
  108.  
  109. <body>
  110.  
  111. <h3>There is a hidden form on this page that contains values From the GA tracking
  112. cookies. Click the button to show the extracted values.</h3>
  113.  
  114. <form method="POST" name='contactform' onSubmit="populateHiddenFields(this);">
  115. <input type='hidden' name='source' />
  116. <input type='hidden' name='medium' />
  117. <input type='hidden' name='term' />
  118. <input type='hidden' name='content' />
  119. <input type='hidden' name='campaign' />
  120. <input type='hidden' name='segment' />
  121. <input type='hidden' name='numVisits' />
  122. <input type='submit' value='Show GA Info' />
  123. </form>
  124.  
  125. </body>
  126.  
  127. </html>

URL: http://cutroni.com/blog/2009/03/18/updated-integrating-google-analytics-with-a-crm/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.