对于Google Analytics事件跟踪,如何捕获<a> tag using onclick event

时间:2017-02-07 19:57:41

标签: javascript-events google-analytics

I have 10 link buttons on my web page. Each button pertains to a unique city. Each link button has an id attribute containing the name of the city. Each link button contains an onclick event which is recorded/tracked in Google Analytics Event Tracking. However, I haven't figured out how to capture the location from the unique id attribute. See example code below.

In short, I want to dynamically capture the value of the id attribute of the tag as the Event Label in Google Analytics? FYI: In the code below, the Event Label is sending event.target.href to GA. This is where I want to capture the id value.

Here's the code:

<a id=“location-city-example” onclick=“ga(‘send’, ‘event’, ‘button’, ‘click’, event.target.href);” href="/apply" class="btn btn-rj">apply now</a>

screenshot of the issue

的ID

1 个答案:

答案 0 :(得分:0)

嗯......我认为这样可行

onclick="ga('send', 'event', 'button', 'click', this.id);"

请注意,我会坚持使用“直接”引号而不是原始代码中的“智能”引号。后者有时会做有趣的事情。