Skip to content

Commit 56a3387

Browse files
committed
optimize shouldSetCookie
1 parent baf0285 commit 56a3387

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/script/src/base.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,27 @@
122122
function init() {
123123
const params = new URLSearchParams(location.search);
124124

125-
const shouldSetCookie = () => {
125+
const shouldSetCookie = (clickId) => {
126+
const existingClickId = cookieManager.get(DUB_ID_VAR);
127+
// only set cookie if there's no existing click id
128+
// or if the attribution model is last-click and the new click id is different from the existing one
126129
return (
127-
!cookieManager.get(DUB_ID_VAR) || ATTRIBUTION_MODEL !== 'first-click'
130+
!existingClickId ||
131+
(ATTRIBUTION_MODEL === 'last-click' && clickId !== existingClickId)
128132
);
129133
};
130134

131135
// Direct click ID in URL
132136
const clickId = params.get(DUB_ID_VAR);
133-
if (clickId && shouldSetCookie()) {
137+
if (clickId && shouldSetCookie(clickId)) {
134138
cookieManager.set(DUB_ID_VAR, clickId);
135139
setPartnerData(clickId);
136140
return;
137141
}
138142

139143
// Track via query param
140144
if (QUERY_PARAM_VALUE && SHORT_DOMAIN) {
141-
if (shouldSetCookie()) {
145+
if (shouldSetCookie(clickId)) {
142146
trackClick(QUERY_PARAM_VALUE);
143147
}
144148
}

0 commit comments

Comments
 (0)