-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
181 lines (144 loc) · 5.19 KB
/
index.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import MemoryStorage from 'memorystorage';
export enum UtmParamEnum {
Source = 'utm_source',
Medium = 'utm_medium',
Campaign = 'utm_campaign',
Content = 'utm_content',
Name = 'utm_name',
Term = 'utm_term',
InitialSource = 'initial_utm_source',
InitialMedium = 'initial_utm_medium',
InitialCampaign = 'initial_utm_campaign',
InitialContent = 'initial_utm_content',
InitialName = 'initial_utm_name',
InitialTerm = 'initial_utm_term',
Gclid = 'gclid',
}
export type UtmParams = Partial<Record<UtmParamEnum, string>>;
/** Its instance allows you to deal with UTM parameters */
export class UtmSynapse {
public static readonly StorageKey: string = 'utmParams';
protected readonly storage: Storage | null;
constructor() {
// In the future we could allow customizing the `sessionStorage` key...
// In case the storage is missing, log this plugin won't work normally
if (typeof Storage === 'undefined') {
console.warn(
"Using the UTM package without having a storage won't work properly"
);
this.storage = new MemoryStorage();
} else {
this.storage = sessionStorage;
}
}
/** Extract UTM parameters from a URL or by default `window.location.href` */
public parse(url?: string): UtmParams | null {
const parsedUrl = new URL(url || window.location.href);
const urlParams = parsedUrl.searchParams;
const utmParams: UtmParams = {};
Object.values(UtmParamEnum).forEach(utmKey => {
const value = urlParams.get(utmKey);
if (value) {
utmParams[utmKey] = value;
}
});
if (!Object.keys(utmParams).length) {
return null;
}
return utmParams;
}
/** Save UTM parameters for later usage */
public save(params: UtmParams): void {
if (!this.storage) {
return;
}
// Load the existing params if any and patch with the input ones
const previousParamsString = this.storage.getItem(UtmSynapse.StorageKey);
const paramsToSave: UtmParams = JSON.parse(JSON.stringify(params));
if (previousParamsString) {
const previousParams: UtmParams = JSON.parse(previousParamsString);
// If any "initial" in the input params, we ignored keeping the previous saved ones
const mapInitialKeys = {
[UtmParamEnum.Source]: UtmParamEnum.InitialSource,
[UtmParamEnum.Medium]: UtmParamEnum.InitialMedium,
[UtmParamEnum.Campaign]: UtmParamEnum.InitialCampaign,
[UtmParamEnum.Content]: UtmParamEnum.InitialContent,
[UtmParamEnum.Name]: UtmParamEnum.InitialName,
[UtmParamEnum.Term]: UtmParamEnum.InitialTerm,
};
const intersectionArray = Object.values(mapInitialKeys).filter(value =>
Object.keys(params).includes(value)
);
if (!intersectionArray.length) {
const paramsToLookFor = Object.keys(mapInitialKeys);
for (const [key, value] of Object.entries(previousParams)) {
if (paramsToLookFor.includes(key)) {
const convertedKey: UtmParamEnum = (mapInitialKeys as any)[key];
paramsToSave[convertedKey] = value;
}
}
}
}
this.storage.setItem(UtmSynapse.StorageKey, JSON.stringify(paramsToSave));
}
/** Load any UTM parameter in the storage */
public load(): UtmParams | null {
if (!this.storage) {
return null;
}
const utmParams = this.storage.getItem(UtmSynapse.StorageKey);
return utmParams ? (JSON.parse(utmParams) as UtmParams) : null;
}
/** Clear the storage of any UTM parameter */
public clear(): void {
if (!this.storage) {
return;
}
this.storage.removeItem(UtmSynapse.StorageKey);
}
/** Remove UTM parameters from an URL */
public trimUrl(url: string): string {
const parsedUrl = new URL(url);
const urlParams = parsedUrl.searchParams;
if (!urlParams) {
return url;
}
for (const paramKey of Object.values(UtmParamEnum)) {
urlParams.delete(paramKey);
}
return parsedUrl.toString();
}
/**
* Will ask the browser to remove UTM parameters without reloading the page.
* Can be useful to avoid users doing copy/paste with UTM parameters
*
* Tip: you should save parameters before doing this (because they would be lost otherwise)
* Note: if the history browser feature is not accessible it won't work
*/
public cleanDisplayedUrl(): void {
if (!window || !history) {
return;
}
const trimmedUrl = this.trimUrl(window.location.href);
const previousState = history.state;
history.replaceState(previousState, '', trimmedUrl);
}
/**
* Will add provided parameters to the URL
*
* Note: in case the URL already contains one of those parameters already, we remove all the original ones before patching (to not mix different analytics sessions)
*/
public setIntoUrl(url: string, params: UtmParams): string {
const cleanUrlToPatch = this.trimUrl(url);
const parsedUrl = new URL(cleanUrlToPatch);
const urlParams = parsedUrl.searchParams;
for (const [key, value] of Object.entries(params)) {
if (value) {
urlParams.append(key, value);
}
}
return parsedUrl.toString();
}
}
/** Default global instance for the ease of use */
export const utmSynapse = new UtmSynapse();