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
|
import { weightedRandom, pickRandom, randomInt, type WeightedOption } from '../utils.js';
export type ReferrerType = 'direct' | 'organic' | 'social' | 'paid' | 'referral';
export interface ReferrerInfo {
type: ReferrerType;
domain: string | null;
path: string | null;
utmSource: string | null;
utmMedium: string | null;
utmCampaign: string | null;
utmContent: string | null;
utmTerm: string | null;
gclid: string | null;
fbclid: string | null;
}
const referrerTypeWeights: WeightedOption<ReferrerType>[] = [
{ value: 'direct', weight: 0.4 },
{ value: 'organic', weight: 0.25 },
{ value: 'social', weight: 0.15 },
{ value: 'paid', weight: 0.1 },
{ value: 'referral', weight: 0.1 },
];
const searchEngines = [
{ domain: 'google.com', path: '/search' },
{ domain: 'bing.com', path: '/search' },
{ domain: 'duckduckgo.com', path: '/' },
{ domain: 'yahoo.com', path: '/search' },
{ domain: 'baidu.com', path: '/s' },
];
const socialPlatforms = [
{ domain: 'twitter.com', path: null },
{ domain: 'x.com', path: null },
{ domain: 'linkedin.com', path: '/feed' },
{ domain: 'facebook.com', path: null },
{ domain: 'reddit.com', path: '/r/programming' },
{ domain: 'news.ycombinator.com', path: '/item' },
{ domain: 'threads.net', path: null },
{ domain: 'bsky.app', path: null },
];
const referralSites = [
{ domain: 'medium.com', path: '/@author/article' },
{ domain: 'dev.to', path: '/post' },
{ domain: 'hashnode.com', path: '/blog' },
{ domain: 'techcrunch.com', path: '/article' },
{ domain: 'producthunt.com', path: '/posts' },
{ domain: 'indiehackers.com', path: '/post' },
];
interface PaidCampaign {
source: string;
medium: string;
campaign: string;
useGclid?: boolean;
useFbclid?: boolean;
}
const paidCampaigns: PaidCampaign[] = [
{ source: 'google', medium: 'cpc', campaign: 'brand_search', useGclid: true },
{ source: 'google', medium: 'cpc', campaign: 'product_awareness', useGclid: true },
{ source: 'facebook', medium: 'paid_social', campaign: 'retargeting', useFbclid: true },
{ source: 'facebook', medium: 'paid_social', campaign: 'lookalike', useFbclid: true },
{ source: 'linkedin', medium: 'cpc', campaign: 'b2b_targeting' },
{ source: 'twitter', medium: 'paid_social', campaign: 'launch_promo' },
];
const organicCampaigns = [
{ source: 'newsletter', medium: 'email', campaign: 'weekly_digest' },
{ source: 'newsletter', medium: 'email', campaign: 'product_update' },
{ source: 'partner', medium: 'referral', campaign: 'integration_launch' },
];
function generateClickId(): string {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < 32; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
export function getRandomReferrer(): ReferrerInfo {
const type = weightedRandom(referrerTypeWeights);
const result: ReferrerInfo = {
type,
domain: null,
path: null,
utmSource: null,
utmMedium: null,
utmCampaign: null,
utmContent: null,
utmTerm: null,
gclid: null,
fbclid: null,
};
switch (type) {
case 'direct':
// No referrer data
break;
case 'organic': {
const engine = pickRandom(searchEngines);
result.domain = engine.domain;
result.path = engine.path;
break;
}
case 'social': {
const platform = pickRandom(socialPlatforms);
result.domain = platform.domain;
result.path = platform.path;
// Some social traffic has UTM params
if (Math.random() < 0.3) {
result.utmSource = platform.domain.replace('.com', '').replace('.net', '');
result.utmMedium = 'social';
}
break;
}
case 'paid': {
const campaign = pickRandom(paidCampaigns);
result.utmSource = campaign.source;
result.utmMedium = campaign.medium;
result.utmCampaign = campaign.campaign;
result.utmContent = `ad_${randomInt(1, 5)}`;
if (campaign.useGclid) {
result.gclid = generateClickId();
result.domain = 'google.com';
result.path = '/search';
} else if (campaign.useFbclid) {
result.fbclid = generateClickId();
result.domain = 'facebook.com';
result.path = null;
}
break;
}
case 'referral': {
// Mix of pure referrals and organic campaigns
if (Math.random() < 0.6) {
const site = pickRandom(referralSites);
result.domain = site.domain;
result.path = site.path;
} else {
const campaign = pickRandom(organicCampaigns);
result.utmSource = campaign.source;
result.utmMedium = campaign.medium;
result.utmCampaign = campaign.campaign;
}
break;
}
}
return result;
}
|