Google analytics click events with jQuery -


i have long list of click events need track such as:

  ga('send', 'event', 'global', 'social action', 'twitter'); 


following format of:

 $('#button').on('click', function() {     ga('send', 'event', 'button', 'click', 'nav-buttons'); }); 

is there way don't have have multiple click events? so, instead of having #button, can pass in element somehow?

use class instead of id, , add class every anchor tag want track.

for example, lets want track navigation:

<ul>     <li><a href='home.html'>home</a></li>     <li><a href='books.html'>books</a></li>     <li><a href='magazines.html'>magazines</a></li> </ul> 

after add html class every anchor tag:

<ul>     <li><a class="gatrack" href='home.html'>home</a></li>     <li><a class="gatrack" href='books.html'>books</a></li>     <li><a class="gatrack" href='magazines.html'>magazines</a></li> </ul> 

then use jquery select class, , use .text() grab text within anchor tag.

 $('.gatrack').on('click', function() {     var name = $(this).text();     ga('send', 'event', 'button', 'click', name); }); 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -