Advertisement
webpagefxjared

ga hidden fields

Jul 20th, 2011
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.16 KB | None | 0 0
  1. <form id="someContactForm" onsubmit="populateHiddenFields(this);"> <!-- on submit populate the google analytic fields -->
  2.  
  3.    <!-- all the other other contact input fields -->
  4.  
  5.    <!-- now the google analytis hidden fields -->
  6.    <input type="hidden" name="source" />
  7.    <input type="hidden" name="medium" />
  8.    <input type="hidden" name="term" />
  9.    <input type="hidden" name="content" />
  10.    <input type="hidden" name="campaign" />
  11.    <input type="hidden" name="segment" />
  12.    <input type="hidden" name="numVisits" />
  13.  
  14. </form>
  15.  
  16. <script type="text/javascript">
  17. //
  18. // This is a function that I "borrowed" from the urchin.js file.
  19. // It parses a string and returns a value.  I used it to get
  20. // data from the __utmz cookie
  21. //
  22. function _uGC(l,n,s) {
  23.  if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
  24.  var i,i2,i3,c="-";
  25.  i=l.indexOf(n);
  26.  i3=n.indexOf("=")+1;
  27.  if (i > -1) {
  28.   i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
  29.  c=l.substring((i+i3),i2);
  30. }
  31. return c;
  32. }
  33.  
  34. //
  35. // Get the __utmz cookie value. This is the cookies that
  36. // stores all campaign information.
  37. //
  38. var z = _uGC(document.cookie, '__utmz=', ';');
  39. //
  40. // The cookie has a number of name-value pairs.
  41. // Each identifies an aspect of the campaign.
  42. //
  43. // utmcsr  = campaign source
  44. // utmcmd  = campaign medium
  45. // utmctr  = campaign term (keyword)
  46. // utmcct  = campaign content  
  47. // utmccn  = campaign name
  48. // utmgclid = unique identifier used when AdWords auto tagging is enabled
  49. //
  50. // This is very basic code. It separates the campaign-tracking cookie
  51. // and populates a variable with each piece of campaign info.
  52. //
  53. var source  = _uGC(z, 'utmcsr=', '|');
  54. var medium  = _uGC(z, 'utmcmd=', '|');
  55. var term    = _uGC(z, 'utmctr=', '|');
  56. var content = _uGC(z, 'utmcct=', '|');
  57. var campaign = _uGC(z, 'utmccn=', '|');
  58. var gclid   = _uGC(z, 'utmgclid=', '|');
  59. //
  60. // The gclid is ONLY present when auto tagging has been enabled.
  61. // All other variables, except the term variable, will be '(not set)'.
  62. // Because the gclid is only present for Google AdWords we can
  63. // populate some other variables that would normally
  64. // be left blank.
  65. //
  66. if (gclid !="-") {
  67.      source = 'google';
  68.      medium = 'cpc';
  69. }
  70. // Data from the custom segmentation cookie can also be passed
  71. // back to your server via a hidden form field
  72. var csegment = _uGC(document.cookie, '__utmv=', ';');
  73. if (csegment != '-') {
  74.      var csegmentex = /[1-9]*?\.(.*)/;
  75.      csegment    = csegment.match(csegmentex);
  76.      csegment    = csegment[1];
  77. } else {
  78.      csegment = '(not set)';
  79. }
  80.  
  81. //
  82. // One more bonus piece of information.  
  83. // We're going to extract the number of visits that the visitor
  84. // has generated.  It's also stored in a cookie, the __utma cookis
  85. //
  86. var a = _uGC(document.cookie, '__utma=', ';');
  87. var aParts = a.split(".");
  88. var nVisits = aParts[5];
  89.  
  90. function populateHiddenFields(f) {
  91.      f.source.value  = source;
  92.      f.medium.value  = medium;
  93.      f.term.value    = term;
  94.      f.content.value = content;
  95.      f.campaign.value = campaign;
  96.      f.segment.value = csegment;
  97.      f.numVisits.value = nVisits;
  98. }
  99. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement