Skip to content

Commit 59d7709

Browse files
add plugin to track UTM parameters
1 parent b9675bb commit 59d7709

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/**
4+
* Plugin Name: Koko Analytics: Track UTM Parameters
5+
*
6+
* Requires 3 events named "UTM Source", "UTM Medium" and "UTM Campaign"
7+
*/
8+
9+
add_action('wp_footer', function () {
10+
?><script>
11+
window.addEventListener('load', function() {
12+
let map = {
13+
'utm_source': 'UTM Source',
14+
'utm_medium': 'UTM Medium',
15+
'utm_campaign': 'UTM Campaign',
16+
};
17+
18+
let queryParams = new URLSearchParams(window.location.search);
19+
let hashParams = new URLSearchParams(window.location.hash.substring(1));
20+
for (let [p, eventName] of Object.entries(map)) {
21+
let value = queryParams.get(p) ?? hashParams.get(p);
22+
if (value) {
23+
window.koko_analytics.trackEvent(eventName, value);
24+
}
25+
}
26+
});
27+
</script><?php
28+
});

0 commit comments

Comments
 (0)