同样是做事件跟踪,可以通过GTM、页面直接添加,可以使用MP协议,这一节主要介绍入门在在页面直接添加的方法,关于事件参数与原理的请自行脑补或看站点其他文章。
- Category:必备参数,一般固定
- Action:必备参数,可变
- Label:可选,可变
- value:可选,可变
- noninteraction:可选
经典版的用法是:
<span style="font-size: 12pt;">_trackEvent(category, action, opt_label, opt_value, opt_noninteraction) </span>
统一版的用法是:
<span style="font-size: 12pt;">onClick=”ga(‘send’, ‘event’, ‘ category’, ‘action’, ‘label’, value, {'NonInteraction':1});” </span>
下面举例如何添加,比如点击某个链接:
经典版的代码格式为:
<span style="font-size: 12pt;">_trackEvent(‘book retailer’, ‘click’, ‘Barnes&Noble’, 5, True) </span>
统一版的代码格式为:
<span style="font-size: 12pt;">onClick="ga('send', 'event', 'book retailer', 'click', 'Barnes&Noble', 5, True);" </span>
上述格式添加在你需要跟综的位置,你点击那里希望它触发就添加在哪里,完整格式的如:
经典版:
<span style="font-size: 12pt;"><a href=”/catalogue/books.html” onClick=”_gaq.push([‘_trackEvent(‘book retailer’, ‘click’, ‘Barnes&Noble’, 5, True]);”>New Release</a> </span>
统一版:
<span style="font-size: 12pt;"><a href=”/catalogue/books.html” onClick="ga('send', 'event', 'book retailer', 'click', 'Barnes&Noble', 5, True);">New Release</a> </span>
进一步还可以将ga()封装成一个函数,然后给onClick调用,封装的函数如:
<span style="font-size: 12pt;">function click_link(){ ga('send', 'event', 'book retailer', 'click', 'Barnes&Noble', 5, True) } </span>
统一版代码变形为:
<span style="font-size: 12pt;"><a href=”/catalogue/books.html” onClick="click_link()">New Release</a> </span>