Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple conditions to track all clicks with Google Analytics
    primarykey
    data
    text
    <p>I'm trying to track all links clicked with Google Analytics as events. So I wrote some jQuery to capture that. I'm just starting with jQuery, so I don't know if I'm doing it the most efficient way so I'd like some feedback or suggestions as to how to improve the code. The simpler and faster it is, the better, as it is tracking analytics.</p> <p><strong>EDIT: Here's the latest code. I can't figure out why the last button and 3rd to last return undefined values. <a href="http://jsfiddle.net/uqj88/" rel="nofollow">http://jsfiddle.net/uqj88/</a></strong></p> <p>Here's the code that I have so far:</p> <pre><code> $(document).ready( function() { var pageURL = window.location.pathname; var linkText; $('a').click(function (e) { if ($(this).text() != ""){ linkText = $(this).text(); } else if (($(this).text() === "") &amp;&amp; ($(this).children("img") != "") &amp;&amp; ($(this).children("img").attr("alt") != "")) { linkText = $(this).children("img").attr("alt"); } else if (($(this).text() === "") &amp;&amp; ($(this).children("img") != "") &amp;&amp; ($(this).children("img").attr("alt") === "")) { linkText = $(this).children("img").attr("src").split("/").pop(); } _gaq.push(['_trackEvent', pageURL, 'click', linkText]); if (($(this).attr('target') != '_blank') || ($(this).attr('target') != '#')) { e.preventDefault(); setTimeout('document.location = "' + $(this).attr('href') + '"', 150); } }); $('button').click(function () { linkText = $(this).text(); _gaq.push(['_trackEvent', pageURL, 'click', linkText]); }); $("input[type='submit']").click(function () { if ($(this).attr("value") != ""){ linkText = $(this).attr("value"); } else if (($(this).attr("value") === "") &amp;&amp; ($(this).prop("id") != "")) { linkText = $(this).prop("id"); } else if (($(this).attr("value") === "") &amp;&amp; ($(this).prop("class") != "")) { linkText = $(this).prop("class"); } else { linkText = "button"; } _gaq.push(['_trackEvent', pageURL, 'click', linkText]); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload