-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathaffiliateLinksUtils.ts
More file actions
139 lines (126 loc) · 3.57 KB
/
Copy pathaffiliateLinksUtils.ts
File metadata and controls
139 lines (126 loc) · 3.57 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
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
import type { ABParticipations } from '../experiments/lib/ab-tests';
export const SKIMLINK_REL = 'sponsored noreferrer noopener';
/** A function to check if a URL represents an affiliate link */
export const isSkimlink = (url?: string): boolean => {
try {
return !!url && new URL(url).host === 'go.skimresources.com';
} catch (err: unknown) {
// If not a valid URL, it won't be an affiliate link
return false;
}
};
export const extractAbTestParticipationFromUrl = (
url: string,
): ABParticipations => {
try {
const xcust = new URL(url).searchParams.get('xcust');
if (!xcust) return {};
const xcustParts = xcust.split('|');
const abParticipationIndex = xcustParts.indexOf('abTestParticipations');
if (abParticipationIndex === -1) return {};
// Get the abTestParticipations value which should be the value after abTestParticipations key
const participationValue = xcustParts[abParticipationIndex + 1];
if (!participationValue) return {};
const participations: ABParticipations = {};
for (const entry of participationValue.split(',')) {
const [testId, variant] = entry.split(':');
if (testId && variant) {
participations[testId] = variant;
}
}
return participations;
} catch (err: unknown) {
return {};
}
};
/** A function to fetch the Skimlinks account ID from the URL to then pass it into the xcust*/
const getSkimlinksAccountId = (url?: string): string => {
try {
if (!url) return '';
const parsedUrl = new URL(url);
return parsedUrl.searchParams.get('id') ?? '';
} catch {
return '';
}
};
/**
* Builds an AB test string by extracting existing participations from the URL set in Frontend,
* merging them with incoming participations, then serializing as test:variant,test2:variant2.
*/
export const buildMergedAbTestString = ({
url,
abTestParticipations,
}: {
url: string;
abTestParticipations?: ABParticipations;
}): string => {
const existingAbTestParticipations = extractAbTestParticipationFromUrl(url);
// Existing URL values take precedence over incoming values.
const mergedAbTestParticipations = {
...abTestParticipations,
...existingAbTestParticipations,
};
return serializeAbTestParticipations(mergedAbTestParticipations);
};
const serializeAbTestParticipations = (
abTestParticipations: ABParticipations,
): string =>
Object.entries(abTestParticipations)
.map(([key, value]) => `${key}:${value}`)
.join(',');
const createXcustValue = ({
referrerDomain,
skimlinksAccountId,
abTestString,
utmParamsString,
xcustComponentId,
}: {
referrerDomain: string;
skimlinksAccountId: string;
abTestString: string;
utmParamsString: string;
xcustComponentId: string | null;
}): string => {
const xcustSegments = [
'referrer',
referrerDomain,
'accountId',
skimlinksAccountId,
];
if (abTestString) {
xcustSegments.push('abTestParticipations', abTestString);
}
if (utmParamsString) {
xcustSegments.push(utmParamsString);
}
if (xcustComponentId) {
xcustSegments.push('componentId', xcustComponentId);
}
return xcustSegments.join('|');
};
export const buildXcustParamForAffiliateLink = ({
url,
abTestParticipations,
utmParamsString,
referrerDomain,
xcustComponentId,
}: {
url: URL;
abTestParticipations?: ABParticipations;
utmParamsString: string;
referrerDomain: string;
xcustComponentId: string | null;
}): string => {
const abTestString = buildMergedAbTestString({
url: url.toString(),
abTestParticipations,
});
const skimlinksAccountId = getSkimlinksAccountId(url.toString());
return createXcustValue({
referrerDomain,
skimlinksAccountId,
abTestString,
utmParamsString,
xcustComponentId,
});
};