-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathpersist-query-params.js
More file actions
102 lines (89 loc) · 2.72 KB
/
persist-query-params.js
File metadata and controls
102 lines (89 loc) · 2.72 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Dynamic Dashboard Link UtmCampaign Parameter Forwarding with Session Persistence
*
* Automatically forwards utm_campaign query parameter from the original landing page URL
* to the Dashboard navigation button and persists it throughout the session.
*
*/
(function() {
'use strict';
// localStorage key for storing utmCampaign
const STORAGE_KEY = 'utmCampaign';
const ONE_MONTH_MS = 30 * 24 * 60 * 60 * 1000;
/**
* Check if current page has a utmCampaign query param and store it in localstorage
*/
function storeUtmCampaign() {
const params = new URLSearchParams(window.location.search);
const utmCampaign = params.get("utm_campaign")
if (utmCampaign) {
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify({ utmCampaign, timestamp: Date.now() }));
} catch (error) {
// Silent error handling
}
}
}
/**
* Retrieve utmCampaign from localStorage
* Deletes the value if it's over 1 month old
* returns string
*/
function getStoredUtmCampaign() {
try {
const { utmCampaign, timestamp } = JSON.parse(localStorage.getItem(STORAGE_KEY))
if (timestamp) {
const age = Date.now() - timestamp;
if (age > ONE_MONTH_MS) {
localStorage.removeItem(STORAGE_KEY);
return "";
}
}
return utmCampaign
} catch (error) {
return "";
}
}
/**
* Update the dashboard link href with the utmCampaign from localStorage as a query parameter when someone triggers a click/mouseover/contextmenu event on a dashboard link
*/
function handleDashboardLinkEvent(event) {
const link = event.target.closest('li.navbar-link a');
if (!link || !link.href || !link.href.includes('dashboard.ngrok.com')) {
return;
}
storeUtmCampaign()
const storedUtmCampaign = getStoredUtmCampaign();
if (!storedUtmCampaign) {
return;
}
try {
// Parse the current dashboard URL
const url = new URL(link.href);
url.searchParams.set('utm_campaign', storedUtmCampaign);
link.href = url.toString();
} catch (error) {
// Silent error handling
}
}
/**
* Initialize: Run on DOM ready
*/
function init() {
if (window.utmForwarderInitialized) {
return;
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
storeUtmCampaign()
});
} else {
storeUtmCampaign()
}
document.addEventListener('click', handleDashboardLinkEvent, true);
document.addEventListener('mouseover', handleDashboardLinkEvent, true);
document.addEventListener('contextmenu', handleDashboardLinkEvent, true);
}
init();
window.utmForwarderInitialized = true;
})();