Skip to content

Commit 8ad06db

Browse files
committed
added google analytics outbound link tracking
1 parent 4218f99 commit 8ad06db

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

templates/analytics-thankful.pug

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,44 @@
44
| window.dataLayer = window.dataLayer || [];
55
| function gtag(){dataLayer.push(arguments);}
66
| gtag('js', new Date());
7-
7+
|
88
| gtag('config', 'UA-121109725-1');
99
|</script>
10+
|
11+
|<script>
12+
|// From: https://support.google.com/analytics/answer/7478520?hl=en
13+
|/**
14+
|* Function that registers a click on an outbound link in Analytics.
15+
|* This function takes a valid URL string as an argument, and uses that URL string
16+
|* as the event label. Setting the transport method to 'beacon' lets the hit be sent
17+
|* using 'navigator.sendBeacon' in browser that support it.
18+
|*/
19+
|var getOutboundLink = function(urlOrEvent) {
20+
| let url;
21+
| let target;
22+
| if(typeof urlOrEvent === 'string') {
23+
| url = urlOrEvent;
24+
| } else {
25+
| let event = urlOrEvent || window.event;
26+
| let el = event.target || event.srcElement;
27+
| if(!el.href) el = el.parentNode;
28+
| url = el.href;
29+
| target = el.target;
30+
| }
31+
| //console.log(url);
32+
| //console.log(target);
33+
| if(window.google_tag_manager) {
34+
| gtag('event', 'click', {
35+
| 'event_category': 'outbound',
36+
| 'event_label': url,
37+
| 'transport_type': 'beacon',
38+
| 'event_callback': function() {
39+
| window.open(url, target);
40+
| //document.location = url;
41+
| }
42+
| });
43+
| } else {
44+
| window.open(url, target);
45+
| }
46+
|}
47+
|</script>

thankful/index.pug

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ html
3737
3838
div(style="display: flex; align-items: center; justify-content: center;")
3939
div(style="margin-right: 1em")
40-
a(href="https://chrome.google.com/webstore/detail/thankful/eapbondnpopbiepnjfhnaaejfdfjhnde")
40+
a(href="https://chrome.google.com/webstore/detail/thankful/eapbondnpopbiepnjfhnaaejfdfjhnde", target="_blank", onclick="getOutboundLink(event); return false;")
4141
img(src="https://developer.chrome.com/webstore/images/ChromeWebStore_BadgeWBorder_v2_206x58.png")
4242
div
43-
a(href="https://addons.mozilla.org/en-US/firefox/addon/getthankful/")
43+
a(href="https://addons.mozilla.org/en-US/firefox/addon/getthankful/", target="_blank", onclick="getOutboundLink(event); return false;")
4444
img(src="https://addons.cdn.mozilla.net/static/img/addons-buttons/AMO-button_1.png")
4545

4646
div.mdl-typography--body-2-color-contrast(style="margin-top: 0.5em")

0 commit comments

Comments
 (0)