-
Notifications
You must be signed in to change notification settings - Fork 3
/
tag-rocket-gtag-link-tracking.html
44 lines (36 loc) · 1.23 KB
/
tag-rocket-gtag-link-tracking.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script data-cfasync="false">
/*
Web Site Advantage: Tag Rocket gtag link tracking [v1.0]
Tracks links based on a CSS selector
https://bigcommerce.websiteadvantage.com.au/
Copyright (C) 2024 Web Site Advantage
*/
!function(w,d){
var linkCssSelector = '.navPages-action';
var eventName = 'click_menu'; // lowercase, alphnumeric, underscores, no spaces
var sendTo = 'GoogleAnalyticsFour'; // Tag Rocket will send this to your GA4 ID
// End of settings --------
var sendEvent = function(link) {
gtag('event', eventName, {
'link_url': link.href,
'link_text': link.innerText,
'send_to': sendTo
});
};
window.onload = function() {
var links = d.querySelectorAll(linkCssSelector);
links.forEach(function(link) {
link.addEventListener('click', function(event) {
if (link.target === '_blank') {
// directly send event
sendEvent(link);
} else {
w.addEventListener('beforeunload', function() {
sendEvent(link);
});
}
});
});
};
}(window, document)
</script>