-
Notifications
You must be signed in to change notification settings - Fork 0
/
format-firebase-dynamic-links.ts
155 lines (133 loc) · 4.75 KB
/
format-firebase-dynamic-links.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
import { Middleware } from '../middleware';
import platform from 'platform';
/** UTM parameters allowed by Firebase */
enum FirebaseUtmParamEnum {
Source = 'utm_source',
Medium = 'utm_medium',
Campaign = 'utm_campaign',
Content = 'utm_content',
Term = 'utm_term',
}
/** Ref: https://firebase.google.com/docs/dynamic-links/create-manually */
export type DynamicLinkParam =
| 'apn'
| 'afl'
| 'amv'
| 'ibi'
| 'ifl'
| 'ius'
| 'ipfl'
| 'ipbi'
| 'isi'
| 'imv'
| 'efr'
| 'ofl'
| 'st'
| 'sd'
| 'si'
| 'utm_source'
| 'utm_medium'
| 'utm_campaign'
| 'utm_term'
| 'utm_content'
| 'at'
| 'ct'
| 'mt'
| 'pt'
| 'd';
export interface FormatFirebaseDynamicLinksOptions {
dynamicLinkBase: string; // E.g. `https://example.page.link`, this comes from Firebase (either their generated URL or a custom domain)
android?: {
storeId: string; // E.g. `com.example.app`
storeLink?: string; // E.g. `https://play.google.com/store/apps/details?id=com.example.app`
};
ios?: {
bundleId: string; // E.g. `com.example.app`
nativeScheme?: string; // E.g. `example` in case you use deep linking like `example://product?id=123`
storeId: string; // E.g. `com.example.app`
storeLink?: string; // E.g. `https://apps.apple.com/us/app/my-app/id0000000000`
skipAppPreviewPage?: string; // Corresponds to the `efr` parameter. Default `1`
};
injectUtmParamsInDynamicLink?: boolean; // The UTM parameters from the hyperlink will be injected into the Dynamic Link. Default `true`
injectUtmParamsInFallback?: boolean; // The UTM paramaters from the hyperlink will be injected into the fallback. Default `true`
usePlatformLinkAsFallback?: boolean; // By default the fallback is the hyperlink but we can force using the `ofl` parameter to redirect to the store website. Default `false`
overrideParams?: Partial<Record<DynamicLinkParam, string>>; // In case you want to use extra parameters of Firebase not yet used in this library you are free to do so in this object
}
/** UTM middleware to inject predefined parameters into links depending on options */
export function FormatFirebaseDynamicLinksMiddleware(
options: FormatFirebaseDynamicLinksOptions
): Middleware {
return (properties, element, next) => {
let url: URL;
try {
url = new URL(properties.href);
} catch (err) {
// In case it's not a valid format the middleware is skipped without affecting next ones
return next();
}
let androidStoreUrl: URL | undefined;
let iosStoreUrl: URL | undefined;
const dynamicLink = new URL(options.dynamicLinkBase);
dynamicLink.searchParams.set('link', url.toString());
if (options.android) {
dynamicLink.searchParams.set('apn', options.android.storeId);
if (options.android.storeLink) {
androidStoreUrl = new URL(options.android.storeLink);
}
}
if (options.ios) {
dynamicLink.searchParams.set('isi', options.ios.storeId);
dynamicLink.searchParams.set('ibi', options.ios.bundleId);
if (options.ios.storeLink) {
iosStoreUrl = new URL(options.ios.storeLink);
}
if (options.ios.nativeScheme) {
dynamicLink.searchParams.set('ius', options.ios.nativeScheme);
}
dynamicLink.searchParams.set(
'efr',
options.ios.skipAppPreviewPage || '1'
);
}
if (
options.injectUtmParamsInDynamicLink ||
options.injectUtmParamsInFallback
) {
const urlParams = url.searchParams;
// Get UTM parameters from the original link
Object.values(FirebaseUtmParamEnum).forEach(utmKey => {
const utmValue = urlParams.get(utmKey);
if (utmValue) {
if (options.injectUtmParamsInDynamicLink) {
dynamicLink.searchParams.set(utmKey, utmValue);
}
if (options.injectUtmParamsInFallback) {
if (androidStoreUrl) {
androidStoreUrl.searchParams.set(utmKey, utmValue);
}
if (iosStoreUrl) {
iosStoreUrl.searchParams.set(utmKey, utmValue);
}
}
}
});
}
// Fallback link (after UTM manipulations because they can be affected)
if (options.usePlatformLinkAsFallback !== false) {
if ((platform as any).os.family === 'iOS' && iosStoreUrl) {
dynamicLink.searchParams.set('ofl', iosStoreUrl.toString());
} else if ((platform as any).os.family === 'Android' && androidStoreUrl) {
dynamicLink.searchParams.set('ofl', androidStoreUrl.toString());
}
}
if (options.overrideParams) {
for (const [paramKey, paramValue] of Object.entries(
options.overrideParams
)) {
dynamicLink.searchParams.set(paramKey, paramValue);
}
}
properties.href = dynamicLink.toString();
next();
};
}