title: Track a button onclick event
body: <p>I have a script that tracks outgoing links when the link is inside an <code>&lt;a&gt;</code> tag.</p>
  
  <p>The script is from <a href="http://www.php-development.ru/php-scripts/click-tracker.php" rel="nofollow">here</a></p>
  
  <p>The script will track html inside the tag or an image.</p>
  
  <p>What I need is for it to track a button as seen below.</p>
  
  <p>Here is the code I guess I need to modify:</p>
  
  <pre><code>    function clicktracker(e)
  {
  var ie  = navigator.appName == "Microsoft Internet Explorer";
  var src = ie ? window.event.srcElement : e.target;
  var tag =  (src.tagName.toLowerCase() != "a") ? src.parentNode : src;
  
  if (!tag || tag.tagName.toLowerCase() != "a") return;
  
  domain    = clicktracker_domain   (tag.href);
  extension = clicktracker_extension(tag.href);
  
  if ( clicktracker_inarray(clicktracker_domains, domain) &amp;&amp;
      !clicktracker_inarray(clicktracker_extensions, extension)) return;
  
  var url   = tag.href;
  var title = '';
  
  if (!title) if (tag.tagName.toLowerCase() ==   "a") title = clicktracker_innertxt(tag.innerHTML);
  if (!title) if (tag.tagName.toLowerCase() ==   "a") title = clicktracker_innertxt(tag.title);
  if (!title) if (src.tagName.toLowerCase() == "img") title = clicktracker_innertxt(src.alt);
  if (!title) if (src.tagName.toLowerCase() == "img") title = clicktracker_innertxt("Image");
  url   = escape(url  .substr(0, 150));
  title = escape(title.substr(0, 150));
  
  if (url &amp;&amp; title) setTimeout("clicktracker_aux('"+url+"', '"+title+"')", 10);
  return;
  }
  </code></pre>
  
  <p>Here is the button I want to track</p>
  
  <pre><code>&lt;button type="button" title="title" style="background:#cda85c;" class="button btn-cart" onclick="window.open('http://www.example.com')"&gt;&lt;span&gt;&lt;span&gt;&lt;?php echo $this-&gt;__('Buy Now') ?&gt;&lt;/span&gt;&lt;/span&gt;&lt;/button&gt;
  </code></pre>
  
  <p>Here is the full script:</p>
  
  <pre><code>function clicktracker_inarray (arr, val)
  {
  for (var i in arr) if (arr[i] == val) return true;
  return false;
  }
  
  // ***** clicktracker_innertxt *****
  
  function clicktracker_innertxt(str)
  {
  str = str.replace(/&lt;[^&gt;]*&gt;/g, ' ');
  str = str.replace(  /&amp;amp;/g, '&amp;');
  str = str.replace( /&amp;nbsp;/g, ' ');
  str = str.replace(   /^\s+/g,  '');
  str = str.replace(   /\s+$/g,  '');
  return str;
  }
  
  
  // ***** URL *******************************************************************
  
  var clicktracker_re_scheme = "^\\w+://";
  var clicktracker_re_folder = "((?:-|\\w|\\.)*)";
  var clicktracker_re_domain = clicktracker_re_scheme+       clicktracker_re_folder;
  var clicktracker_re_urlall = clicktracker_re_domain+"(?:/"+clicktracker_re_folder+')*';
  
  // ***** clicktracker_domain *****
  
  function clicktracker_domain(url)
  {
  var reg   = new RegExp(clicktracker_re_domain);
  var match = reg.exec(url);
  if (!match) return "";
  match = match[match.length-1];
  return match;
  }
  
  // ***** clicktracker_extension *****
  
  function clicktracker_extension(url)
  {
  var reg   = new RegExp(clicktracker_re_urlall);
  var match = reg.exec(url);
  if (!match) return "";
  match = match[match.length-1].split(".");
  return (match.length &gt;= 2) ? match[match.length-1] : "";
  }
  
  
  // ***** Track *****************************************************************
  
  // ***** clicktracker_aux *****
  
  function clicktracker_aux(url, title)
  {
  var img = new Image();
  img.src = clicktracker_url+"?url="+url+"&amp;title="+title+"&amp;rand="+Math.random();
  }
  
  // ***** clicktracker *****
  
  function clicktracker(e)
  {
  var ie  = navigator.appName == "Microsoft Internet Explorer";
  var src = ie ? window.event.srcElement : e.target;
  var tag =  (src.tagName.toLowerCase() != "a") ? src.parentNode : src;
  
  if (!tag || tag.tagName.toLowerCase() != "a") return;
  
  domain    = clicktracker_domain   (tag.href);
  extension = clicktracker_extension(tag.href);
  
  if ( clicktracker_inarray(clicktracker_domains, domain) &amp;&amp;
      !clicktracker_inarray(clicktracker_extensions, extension)) return;
  
  var url   = tag.href;
  var title = '';
  
  if (!title) if (tag.tagName.toLowerCase() ==   "a") title = clicktracker_innertxt(tag.innerHTML);
  if (!title) if (tag.tagName.toLowerCase() ==   "a") title = clicktracker_innertxt(tag.title);
  if (!title) if (src.tagName.toLowerCase() == "img") title = clicktracker_innertxt(src.alt);
  if (!title) if (src.tagName.toLowerCase() == "img") title = clicktracker_innertxt("Image");
  url   = escape(url  .substr(0, 150));
  title = escape(title.substr(0, 150));
  
  if (url &amp;&amp; title) setTimeout("clicktracker_aux('"+url+"', '"+title+"')", 10);
  return;
  }
  
  
  // ***** Attach Events *********************************************************
  
  if (navigator.appName == "Microsoft Internet Explorer")
      document.attachEvent   ('onclick', clicktracker);
  else document.addEventListener('click', clicktracker, false);
  </code></pre>
  
  <p>edit*****</p>
  
  <p>The clicks are then stored in the database and called for from a php page.</p>