Google Analytics tracking link click and form submit to multiple trackers


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

<p>usage:
<code>&lt;a href="" onclick="recordOutboundLink(link, category, action, opt_label, opt_value);return false;"&gt;
&lt;form id="uniqueID" onsubmit="trackFormSubmit(form, category, action, opt_label, opt_value);return false;"&gt;</code></p>


Copy this code and paste it in your HTML
  1. var arrTracker = ['trackerName1', 'trackerName2']; // tracker names
  2. // tracking form submit
  3. function trackFormSubmit(form, category, action, opt_label, opt_value) {
  4. for (var i = 0, ii = arrTracker.length; i < ii; i++) {
  5. _gat._getTrackerByName(arrTracker[i])._trackEvent(category, action, opt_label, opt_value);
  6. }
  7. setTimeout("document.getElementById('"+form.id+"').submit();", 100);
  8. return false;
  9. }
  10.  
  11. // tracking link click
  12. function recordOutboundLink(link, category, action, opt_label, opt_value) {
  13. for (var i = 0, ii = arrTracker.length; i < ii; i++) {
  14. _gat._getTrackerByName(arrTracker[i])._trackEvent(category, action, opt_label, opt_value);
  15. }
  16. setTimeout('document.location = "' + link.href + '"', 100);
  17. return false;
  18. }
  19.  
  20. <a href="" onclick="recordOutboundLink(link, category, action, opt_label, opt_value);return false;">
  21. <form id="uniqueID" onsubmit="trackFormSubmit(form, category, action, opt_label, opt_value);return false;">

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.