-2

I am manifest and js file in my chrome extension
code in js file:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'My account No.']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  ga.src = 'https://ssl.google-analytics.com/ga.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


// MAIN CODE:

     var main_div = document.getElementsByClassName('someclass');
        var download_button = document.createElement('a');
        download_button.setAttribute("id", "download");
        download_button.style.color = "blue";
        download_button.href = "#";

        download_button.innerHTML = "Download";


        main_div[0].appendChild(download_button);
        _gaq.push(['_trackEvent', 'download', 'clicked']);
        download_button.addEventListener('click', Download_Answer, false);

I want everytime the user click download button the
google analytic should track this event how to do this.I am not getting my results

1 Answers1

1

Here's a look at using Google’s Universal Analytics tracking to a Chrome extension.

Assuming you have given Google Analytics the correct permissions in manifest.json, add the following to setup Google Analytics:

// Standard Google Universal Analytics code
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); // Note: https protocol here

ga('create', 'UA-XXXXX-YY', 'auto');
ga('set', 'checkProtocolTask', function(){}); // Removes failing protocol check. @see: http://stackoverflow.com/a/22152353/1958200
ga('send', 'pageview', '/options.html'); // A virtual pageview on your options page

Using jQuery, I'd then assign click events similar to this:

$('#someButton').click(function () {
  ga('send', 'event', 'button', 'click', 'someButtonAction');
  // remaining logic for button click
});
dvdsmpsn
  • 2,839
  • 1
  • 26
  • 40