Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions packages/script/src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,44 @@
const SHORT_DOMAIN = DOMAINS_CONFIG.refer;
const ATTRIBUTION_MODEL =
script.getAttribute('data-attribution-model') || 'last-click';
const QUERY_PARAM = script.getAttribute('data-query-param') || 'via';
const QUERY_PARAM_VALUE = new URLSearchParams(location.search).get(
QUERY_PARAM,
);

// Resolve query params from data-query-param and data-query-params
const QUERY_PARAMS = (() => {
const queryParam = script.getAttribute('data-query-param');
const queryParams = script.getAttribute('data-query-params');

let resolvedQueryParams = ['via'];

if (queryParam) {
resolvedQueryParams = [queryParam, ...resolvedQueryParams];
} else if (queryParams) {
try {
resolvedQueryParams = [
...JSON.parse(queryParams),
...resolvedQueryParams,
];
} catch (error) {
console.warn(
'[dubAnalytics] Failed to parse data-query-params.',
error,
);
}
}

return [...new Set(resolvedQueryParams)];
})();

const QUERY_PARAM_VALUE = (() => {
const params = new URLSearchParams(location.search);

for (const param of QUERY_PARAMS) {
if (params.get(param)) {
return params.get(param);
}
}

return null;
})();

// Initialize global DubAnalytics object
window.DubAnalytics = window.DubAnalytics || {
Expand Down Expand Up @@ -262,18 +296,15 @@

// Export minimal API with minified names
window._dubAnalytics = {
c: cookieManager, // was cookieManager
i: DUB_ID_VAR, // was DUB_ID_VAR
h: HOSTNAME, // was HOSTNAME
a: API_HOST, // was API_HOST
o: COOKIE_OPTIONS, // was COOKIE_OPTIONS
d: SHORT_DOMAIN, // was SHORT_DOMAIN
m: ATTRIBUTION_MODEL, // was ATTRIBUTION_MODEL
p: QUERY_PARAM, // was QUERY_PARAM
v: QUERY_PARAM_VALUE, // was QUERY_PARAM_VALUE
n: DOMAINS_CONFIG, // was DOMAINS_CONFIG
k: PUBLISHABLE_KEY,
c: cookieManager,
qm: queueManager,
i: DUB_ID_VAR,
h: HOSTNAME,
a: API_HOST,
d: SHORT_DOMAIN,
v: QUERY_PARAM_VALUE,
n: DOMAINS_CONFIG,
k: PUBLISHABLE_KEY,
};

// Initialize
Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ function inject(props: AnalyticsProps): void {
script.setAttribute('data-query-param', props.queryParam);
}

if (props.queryParams) {
script.setAttribute('data-query-params', JSON.stringify(props.queryParams));
}

if (props.scriptProps) {
const { src: _, ...restProps } = props.scriptProps; // we already set the src above
Object.assign(script, restProps);
Expand Down
8 changes: 8 additions & 0 deletions packages/web/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,17 @@ export interface AnalyticsProps {
* The query parameter to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`).
*
* @default 'via'
* @deprecated Use queryParams instead
*/
queryParam?: string;

/**
* The list of query parameters to listen to for client-side click-tracking (e.g. `?via=john`, `?ref=jane`).
*
* @default ['via']
*/
queryParams?: string[];

/**
* Custom properties to pass to the script.
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement
Expand Down