Skip to content

Commit 8b88934

Browse files
authored
Merge pull request #66 from dubinc/support-queryParams
Add support for `queryParams`
2 parents 565567c + ac9ef25 commit 8b88934

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

packages/script/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dub/analytics-script",
3-
"version": "0.0.31",
3+
"version": "0.0.32",
44
"main": "src/index.js",
55
"files": [
66
"dist/analytics/script.js",

packages/script/src/base.js

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,44 @@
6262
const SHORT_DOMAIN = DOMAINS_CONFIG.refer;
6363
const ATTRIBUTION_MODEL =
6464
script.getAttribute('data-attribution-model') || 'last-click';
65-
const QUERY_PARAM = script.getAttribute('data-query-param') || 'via';
66-
const QUERY_PARAM_VALUE = new URLSearchParams(location.search).get(
67-
QUERY_PARAM,
68-
);
65+
66+
// Resolve query params from data-query-param and data-query-params
67+
const QUERY_PARAMS = (() => {
68+
const queryParam = script.getAttribute('data-query-param');
69+
const queryParams = script.getAttribute('data-query-params');
70+
71+
let resolvedQueryParams = ['via'];
72+
73+
if (queryParam) {
74+
resolvedQueryParams = [queryParam, ...resolvedQueryParams];
75+
} else if (queryParams) {
76+
try {
77+
resolvedQueryParams = [
78+
...JSON.parse(queryParams),
79+
...resolvedQueryParams,
80+
];
81+
} catch (error) {
82+
console.warn(
83+
'[dubAnalytics] Failed to parse data-query-params.',
84+
error,
85+
);
86+
}
87+
}
88+
89+
return [...new Set(resolvedQueryParams)];
90+
})();
91+
92+
const QUERY_PARAM_VALUE = (() => {
93+
const params = new URLSearchParams(location.search);
94+
95+
for (const param of QUERY_PARAMS) {
96+
if (params.get(param)) {
97+
return params.get(param);
98+
}
99+
}
100+
101+
return null;
102+
})();
69103

70104
// Initialize global DubAnalytics object
71105
window.DubAnalytics = window.DubAnalytics || {
@@ -262,18 +296,15 @@
262296

263297
// Export minimal API with minified names
264298
window._dubAnalytics = {
265-
c: cookieManager, // was cookieManager
266-
i: DUB_ID_VAR, // was DUB_ID_VAR
267-
h: HOSTNAME, // was HOSTNAME
268-
a: API_HOST, // was API_HOST
269-
o: COOKIE_OPTIONS, // was COOKIE_OPTIONS
270-
d: SHORT_DOMAIN, // was SHORT_DOMAIN
271-
m: ATTRIBUTION_MODEL, // was ATTRIBUTION_MODEL
272-
p: QUERY_PARAM, // was QUERY_PARAM
273-
v: QUERY_PARAM_VALUE, // was QUERY_PARAM_VALUE
274-
n: DOMAINS_CONFIG, // was DOMAINS_CONFIG
275-
k: PUBLISHABLE_KEY,
299+
c: cookieManager,
276300
qm: queueManager,
301+
i: DUB_ID_VAR,
302+
h: HOSTNAME,
303+
a: API_HOST,
304+
d: SHORT_DOMAIN,
305+
v: QUERY_PARAM_VALUE,
306+
n: DOMAINS_CONFIG,
307+
k: PUBLISHABLE_KEY,
277308
};
278309

279310
// Initialize

packages/web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dub/analytics",
3-
"version": "0.0.31",
3+
"version": "0.0.32",
44
"description": "",
55
"keywords": [
66
"analytics",

packages/web/src/generic.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ function inject(props: AnalyticsProps): void {
7979
script.setAttribute('data-query-param', props.queryParam);
8080
}
8181

82+
if (props.queryParams) {
83+
script.setAttribute('data-query-params', JSON.stringify(props.queryParams));
84+
}
85+
8286
if (props.scriptProps) {
8387
const { src: _, ...restProps } = props.scriptProps; // we already set the src above
8488
Object.assign(script, restProps);

packages/web/src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,17 @@ export interface AnalyticsProps {
143143
* The query parameter to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`).
144144
*
145145
* @default 'via'
146+
* @deprecated Use queryParams instead
146147
*/
147148
queryParam?: string;
148149

150+
/**
151+
* The list of query parameters to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`).
152+
*
153+
* @default ['via']
154+
*/
155+
queryParams?: string[];
156+
149157
/**
150158
* Custom properties to pass to the script.
151159
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement

0 commit comments

Comments
 (0)