/
Ref.ts
30 lines (23 loc) · 792 Bytes
/
Ref.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
import { InteractionMiddleware, Interaction } from '../types';
/**
* Turns URLs such as example.com?ref=foobar into referrals
*/
const ref: InteractionMiddleware = (currentInteraction: Interaction): Interaction => {
// If it is already attributed to something just return that
if (currentInteraction.source && currentInteraction.medium) {
return currentInteraction;
}
const {ref, ...additionalParameters} = currentInteraction.parameters ?? {};
if (!ref) {
return currentInteraction;
}
const interaction: Interaction = {
source: ref,
medium: 'referral',
};
if (Object.keys(additionalParameters).length > 0) {
interaction.parameters = additionalParameters;
}
return interaction;
}
export default ref;