Skip to content

Commit c8443a5

Browse files
committed
refactor: Simplify cookie sync manager
1 parent 7681164 commit c8443a5

File tree

6 files changed

+141
-248
lines changed

6 files changed

+141
-248
lines changed

src/cookieSyncManager.ts

Lines changed: 54 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Dictionary, isEmpty, replaceAmpWithAmpersand, replaceMPID } from './utils';
1+
import {
2+
Dictionary,
3+
isEmpty,
4+
replaceAmpWithAmpersand,
5+
combineUrlWithRedirect,
6+
} from './utils';
27
import Constants from './constants';
38
import { MParticleWebSDK } from './sdkRuntimeModels';
49
import { MPID } from '@mparticle/web-sdk';
@@ -25,7 +30,6 @@ export interface IPixelConfiguration {
2530
}
2631
export interface ICookieSyncManager {
2732
attemptCookieSync: (
28-
previousMPID: MPID,
2933
mpid: MPID,
3034
mpidIsNotInCookies?: boolean
3135
) => void;
@@ -34,7 +38,6 @@ export interface ICookieSyncManager {
3438
moduleId: string,
3539
mpid: MPID,
3640
cookieSyncDates: CookieSyncDates,
37-
filteringConsentRuleValues: IConsentRules,
3841
mpidIsNotInCookies: boolean,
3942
requiresConsent: boolean
4043
) => void;
@@ -47,17 +50,19 @@ export interface ICookieSyncManager {
4750

4851
const hasFrequencyCapExpired = (
4952
frequencyCap: number,
50-
lastSyncDate?: number,
53+
lastSyncDate?: number
5154
): boolean => {
55+
// If there is no lastSyncDate, then there is no previous cookie sync, so we should sync the cookie
5256
if (!lastSyncDate) {
5357
return true;
5458
}
5559

60+
// Otherwise, compare the last sync date to determine if it should do a cookie sync again
5661
return (
5762
new Date().getTime() >
5863
new Date(lastSyncDate).getTime() + frequencyCap * DAYS_IN_MILLISECONDS
5964
);
60-
}
65+
};
6166

6267
export default function CookieSyncManager(
6368
this: ICookieSyncManager,
@@ -67,17 +72,20 @@ export default function CookieSyncManager(
6772

6873
// Public
6974
this.attemptCookieSync = (
70-
previousMPID: MPID,
7175
mpid: MPID,
7276
mpidIsNotInCookies?: boolean
7377
): void => {
7478
if (!mpid || mpInstance._Store.webviewBridgeEnabled) {
7579
return;
7680
}
7781

78-
const { pixelConfigurations } = mpInstance._Store;
7982
const persistence = mpInstance._Persistence.getPersistence();
8083

84+
if (isEmpty(persistence)) {
85+
return;
86+
}
87+
88+
const { pixelConfigurations } = mpInstance._Store;
8189
pixelConfigurations.forEach((pixelSettings: IPixelConfiguration) => {
8290
// set requiresConsent to false to start each additional pixel configuration
8391
// set to true only if filteringConsenRuleValues.values.length exists
@@ -91,6 +99,17 @@ export default function CookieSyncManager(
9199
requiresConsent = true;
92100
}
93101

102+
// if MPID is new to cookies, we should not try to perform the cookie sync
103+
// because a cookie sync can only occur once a user either consents or doesn't
104+
// we should not check if its enabled if the user has a blank consent
105+
if (requiresConsent && mpidIsNotInCookies) {
106+
return;
107+
}
108+
109+
if (isEmpty(pixelSettings.pixelUrl) && isEmpty(pixelSettings.redirectUrl)) {
110+
return;
111+
}
112+
94113
// Kit Module ID
95114
const moduleId = pixelSettings.moduleId.toString();
96115

@@ -104,98 +123,58 @@ export default function CookieSyncManager(
104123
? replaceAmpWithAmpersand(pixelSettings.redirectUrl)
105124
: null;
106125

107-
const urlWithRedirect = this.combineUrlWithRedirect(
126+
const urlWithRedirect = combineUrlWithRedirect(
108127
mpid,
109128
pixelUrl,
110129
redirectUrl
111130
);
112131

113-
// reset shouldPerformCookieSync to false
114-
let shouldPerformCookieSync = false;
115-
if (previousMPID && previousMPID !== mpid) {
116-
if (persistence && persistence[mpid]) {
117-
if (!persistence[mpid].csd) {
118-
persistence[mpid].csd = {};
119-
}
120-
shouldPerformCookieSync = true;
121-
}
122-
} else {
123-
if (!persistence || !persistence[mpid]) {
124-
return;
125-
}
126-
132+
// set up csd object if it doesn't exist
133+
if (persistence && persistence[mpid]) {
127134
if (!persistence[mpid].csd) {
128135
persistence[mpid].csd = {};
129136
}
137+
}
130138

131-
const lastSyncDateForModule = persistence[mpid].csd[moduleId] || null;
139+
const lastSyncDateForModule = persistence[mpid].csd[moduleId] || null;
132140

133-
// Check to see if we need to refresh cookieSync
134-
if (hasFrequencyCapExpired(frequencyCap, lastSyncDateForModule)) {
135-
shouldPerformCookieSync = true;
136-
}
141+
const { isEnabledForUserConsent } = mpInstance._Consent;
142+
if (!isEnabledForUserConsent(filteringConsentRuleValues, mpInstance.Identity.getCurrentUser())) {
143+
return;
137144
}
138145

139-
if (shouldPerformCookieSync) {
140-
self.performCookieSync(
141-
urlWithRedirect,
142-
moduleId,
143-
mpid,
144-
persistence[mpid].csd,
145-
filteringConsentRuleValues,
146-
mpidIsNotInCookies,
147-
requiresConsent
148-
);
146+
if (!hasFrequencyCapExpired(frequencyCap, lastSyncDateForModule)) {
147+
return;
149148
}
150149

150+
self.performCookieSync(
151+
urlWithRedirect,
152+
moduleId,
153+
mpid,
154+
persistence[mpid].csd,
155+
mpidIsNotInCookies,
156+
requiresConsent
157+
);
151158
});
152159
};
153160

154-
this.combineUrlWithRedirect = (
155-
mpid: MPID,
156-
pixelUrl: string,
157-
redirectUrl: string,
158-
): string => {
159-
const url = replaceMPID(pixelUrl, mpid);
160-
const redirect = redirectUrl ? replaceMPID(redirectUrl, mpid) : '';
161-
return url + encodeURIComponent(redirect);
162-
};
163-
164161
// Private
165162
this.performCookieSync = (
166163
url: string,
167164
moduleId: string,
168165
mpid: MPID,
169166
cookieSyncDates: CookieSyncDates,
170-
filteringConsentRuleValues: IConsentRules,
171-
mpidIsNotInCookies: boolean,
172-
requiresConsent: boolean
173167
): void => {
174-
// if MPID is new to cookies, we should not try to perform the cookie sync
175-
// because a cookie sync can only occur once a user either consents or doesn't
176-
// we should not check if its enabled if the user has a blank consent
177-
if (requiresConsent && mpidIsNotInCookies) {
178-
return;
179-
}
168+
const img = document.createElement('img');
180169

181-
if (
182-
// https://go.mparticle.com/work/SQDSDKS-5009
183-
mpInstance._Consent.isEnabledForUserConsent(
184-
filteringConsentRuleValues,
185-
mpInstance.Identity.getCurrentUser()
186-
)
187-
) {
188-
const img = document.createElement('img');
189-
190-
mpInstance.Logger.verbose(InformationMessages.CookieSync);
191-
img.onload = function() {
192-
cookieSyncDates[moduleId] = new Date().getTime();
193-
mpInstance._Persistence.saveUserCookieSyncDatesToPersistence(
194-
mpid,
195-
cookieSyncDates
196-
);
197-
};
198-
img.src = url;
199-
}
170+
mpInstance.Logger.verbose(InformationMessages.CookieSync);
171+
img.onload = function() {
172+
cookieSyncDates[moduleId] = new Date().getTime();
173+
mpInstance._Persistence.saveUserCookieSyncDatesToPersistence(
174+
mpid,
175+
cookieSyncDates
176+
);
177+
};
178+
img.src = url;
200179
};
201180
}

src/identity.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,10 +1230,7 @@ export default function Identity(mpInstance) {
12301230
this.getUserIdentities().userIdentities,
12311231
mpInstance._APIClient.prepareForwardingStats
12321232
);
1233-
mpInstance._CookieSyncManager.attemptCookieSync(
1234-
null,
1235-
this.getMPID()
1236-
);
1233+
mpInstance._CookieSyncManager.attemptCookieSync(this.getMPID());
12371234
},
12381235
isLoggedIn: function() {
12391236
return isLoggedIn;
@@ -1562,7 +1559,6 @@ export default function Identity(mpInstance) {
15621559
);
15631560

15641561
mpInstance._CookieSyncManager.attemptCookieSync(
1565-
previousMPID,
15661562
identityApiResult.mpid,
15671563
mpidIsNotInCookies
15681564
);

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,16 @@ const replaceMPID = (value: string, mpid: MPID): string => value.replace('%%mpid
180180

181181
const replaceAmpWithAmpersand = (value: string): string => value.replace(/&/g, '&');
182182

183+
const combineUrlWithRedirect = (
184+
mpid: MPID,
185+
pixelUrl: string,
186+
redirectUrl: string,
187+
): string => {
188+
const url = replaceMPID(pixelUrl, mpid);
189+
const redirect = redirectUrl ? replaceMPID(redirectUrl, mpid) : '';
190+
return url + encodeURIComponent(redirect);
191+
};
192+
183193
// FIXME: REFACTOR for V3
184194
// only used in store.js to sanitize server-side formatting of
185195
// booleans when checking for `isDevelopmentMode`
@@ -373,4 +383,5 @@ export {
373383
getHref,
374384
replaceMPID,
375385
replaceAmpWithAmpersand,
386+
combineUrlWithRedirect,
376387
};

0 commit comments

Comments
 (0)