Skip to content

Commit e558f2d

Browse files
committed
Add support for queryParams
1 parent 565567c commit e558f2d

3 files changed

Lines changed: 51 additions & 5 deletions

File tree

packages/script/src/base.js

Lines changed: 39 additions & 5 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 || {
@@ -269,7 +303,7 @@
269303
o: COOKIE_OPTIONS, // was COOKIE_OPTIONS
270304
d: SHORT_DOMAIN, // was SHORT_DOMAIN
271305
m: ATTRIBUTION_MODEL, // was ATTRIBUTION_MODEL
272-
p: QUERY_PARAM, // was QUERY_PARAM
306+
p: QUERY_PARAMS, // was QUERY_PARAM
273307
v: QUERY_PARAM_VALUE, // was QUERY_PARAM_VALUE
274308
n: DOMAINS_CONFIG, // was DOMAINS_CONFIG
275309
k: PUBLISHABLE_KEY,

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)