简体   繁体   中英

Google Analytics event tracking on a button

In my Django app, I have a button in a template for every doctor profile that shows the phone number when the button is clicked. I'd like to add Google Analytics event tracking to track how many times people click on the show phone number button for a particular doctor.

<input onclick="change()"  class="btn btn-primary phone" type="button" value="Show Phone Number" id="myButton1" />

      <script type="text/javascript">
      function change() 
{
    var elem = document.getElementById("myButton1");

    elem.value = "" + {{doctor.clinic.contact_no}};
}
      </script>

I found some documentation that talks about how to track links, but am not sure how to do it for a button.

<a href=”/downloads/whitepapers/seo-is-great.pdf” onClick=”_gaq.push([‘_trackEvent’, ‘whitepaper’, ‘download’, ‘SEO is great’, 5, true]);” target=”_blank”>SEO Is Great Whitepaper</a>

Add the ga event tracking code to the change() function. You can include the doctor.clinic.contact_no as one of the parameters to the _trackEvent function, here it would show under "label".

If you are using classic ga (ie have ga.js)

function change() 
{
var elem = document.getElementById("myButton1");
elem.value = "" + {{doctor.clinic.contact_no}};
_gaq.push(['_trackEvent', 'phone', 'show','{{doctor.clinic.contact_no}}',, false]);
}

With Universal analytics (analytics.js) it would be:

function change() 
{
var elem = document.getElementById("myButton1");
elem.value = "" + {{doctor.clinic.contact_no}};
ga('send', 'event', 'phone', 'show','{{doctor.clinic.contact_no}}');
}

Make sure you have the basic tracking code in place as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM