简体   繁体   English

按钮上的Google Analytics(分析)事件跟踪

[英]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. 在我的Django应用中,每个医生个人资料的模板中都有一个按钮,单击该按钮时会显示电话号码。 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. 我想添加Google Analytics(分析)事件跟踪,以跟踪人们单击特定医生的“显示电话号码”按钮的次数。

<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. 将ga事件跟踪代码添加到change()函数。 You can include the doctor.clinic.contact_no as one of the parameters to the _trackEvent function, here it would show under "label". 您可以将doctor.clinic.contact_no作为_trackEvent函数的参数之一,这里将显示在“标签”下。

If you are using classic ga (ie have ga.js) 如果您使用的是经典ga(即拥有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: 使用通用分析(analytics.js),它将是:

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. 确保您还具有基本的跟踪代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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