@@ -50,12 +50,6 @@ ConsentHandler.prototype.getUserConsentState = function () {
50
50
return userConsentState ;
51
51
} ;
52
52
53
- ConsentHandler . prototype . getEventConsentState = function ( eventConsentState ) {
54
- return eventConsentState && eventConsentState . getGDPRConsentState
55
- ? eventConsentState . getGDPRConsentState ( )
56
- : { } ;
57
- } ;
58
-
59
53
ConsentHandler . prototype . getConsentSettings = function ( ) {
60
54
var consentSettings = { } ;
61
55
@@ -258,6 +252,42 @@ Common.prototype.limitProductAttributes = function (attributes) {
258
252
return this . mergeObjects ( limitedProductAttributes , reservedAttributes ) ;
259
253
} ;
260
254
255
+ Common . prototype . getEventConsentState = function ( eventConsentState ) {
256
+ return eventConsentState && eventConsentState . getGDPRConsentState
257
+ ? eventConsentState . getGDPRConsentState ( )
258
+ : { } ;
259
+ } ;
260
+
261
+ Common . prototype . maybeSendConsentUpdateToGoogle = function ( consentState ) {
262
+ // If consent payload is empty,
263
+ // we never sent an initial default consent state
264
+ // so we shouldn't send an update.
265
+ if (
266
+ this . consentPayloadAsString &&
267
+ this . consentMappings &&
268
+ ! this . isEmpty ( consentState )
269
+ ) {
270
+ var updatedConsentPayload =
271
+ this . consentHandler . generateConsentStatePayloadFromMappings (
272
+ consentState ,
273
+ this . consentMappings
274
+ ) ;
275
+
276
+ var eventConsentAsString = JSON . stringify ( updatedConsentPayload ) ;
277
+
278
+ if ( eventConsentAsString !== this . consentPayloadAsString ) {
279
+ gtag ( 'consent' , 'update' , updatedConsentPayload ) ;
280
+ this . consentPayloadAsString = eventConsentAsString ;
281
+ }
282
+ }
283
+ } ;
284
+
285
+ Common . prototype . sendDefaultConsentPayloadToGoogle = function ( consentPayload ) {
286
+ this . consentPayloadAsString = JSON . stringify ( consentPayload ) ;
287
+
288
+ gtag ( 'consent' , 'default' , consentPayload ) ;
289
+ } ;
290
+
261
291
Common . prototype . truncateEventName = function ( eventName ) {
262
292
return truncateString ( eventName , EVENT_NAME_MAX_LENGTH ) ;
263
293
} ;
@@ -1017,32 +1047,6 @@ function EventHandler(common) {
1017
1047
this . common = common || { } ;
1018
1048
}
1019
1049
1020
- EventHandler . prototype . maybeSendConsentUpdateToGa4 = function ( event ) {
1021
- // If consent payload is empty,
1022
- // we never sent an initial default consent state
1023
- // so we shouldn't send an update.
1024
- if ( this . common . consentPayloadAsString && this . common . consentMappings ) {
1025
- var eventConsentState = this . common . consentHandler . getEventConsentState (
1026
- event . ConsentState
1027
- ) ;
1028
-
1029
- if ( ! this . common . isEmpty ( eventConsentState ) ) {
1030
- var updatedConsentPayload =
1031
- this . common . consentHandler . generateConsentStatePayloadFromMappings (
1032
- eventConsentState ,
1033
- this . common . consentMappings
1034
- ) ;
1035
-
1036
- var eventConsentAsString = JSON . stringify ( updatedConsentPayload ) ;
1037
-
1038
- if ( eventConsentAsString !== this . common . consentPayloadAsString ) {
1039
- gtag ( 'consent' , 'update' , updatedConsentPayload ) ;
1040
- this . common . consentPayloadAsString = eventConsentAsString ;
1041
- }
1042
- }
1043
- }
1044
- } ;
1045
-
1046
1050
// TODO: https://mparticle-eng.atlassian.net/browse/SQDSDKS-5715
1047
1051
EventHandler . prototype . sendEventToGA4 = function ( eventName , eventAttributes ) {
1048
1052
var standardizedEventName ;
@@ -1073,7 +1077,10 @@ EventHandler.prototype.sendEventToGA4 = function (eventName, eventAttributes) {
1073
1077
} ;
1074
1078
1075
1079
EventHandler . prototype . logEvent = function ( event ) {
1076
- this . maybeSendConsentUpdateToGa4 ( event ) ;
1080
+ var eventConsentState = this . common . getEventConsentState (
1081
+ event . ConsentState
1082
+ ) ;
1083
+ this . common . maybeSendConsentUpdateToGoogle ( eventConsentState ) ;
1077
1084
this . sendEventToGA4 ( event . EventName , event . EventAttributes ) ;
1078
1085
} ;
1079
1086
@@ -1124,7 +1131,10 @@ EventHandler.prototype.logPageView = function (event) {
1124
1131
event . EventAttributes
1125
1132
) ;
1126
1133
1127
- this . maybeSendConsentUpdateToGa4 ( event ) ;
1134
+ var eventConsentState = this . common . getEventConsentState (
1135
+ event . ConsentState
1136
+ ) ;
1137
+ this . common . maybeSendConsentUpdateToGoogle ( eventConsentState ) ;
1128
1138
this . sendEventToGA4 ( 'page_view' , eventAttributes ) ;
1129
1139
1130
1140
return true ;
@@ -1303,22 +1313,29 @@ var initialization = {
1303
1313
1304
1314
common . consentPayloadDefaults =
1305
1315
common . consentHandler . getConsentSettings ( ) ;
1306
- var initialConsentState = common . consentHandler . getUserConsentState ( ) ;
1307
-
1308
- var defaultConsentPayload =
1316
+ var defaultConsentPayload = common . cloneObject (
1317
+ common . consentPayloadDefaults
1318
+ ) ;
1319
+ var updatedConsentState = common . consentHandler . getUserConsentState ( ) ;
1320
+ var updatedDefaultConsentPayload =
1309
1321
common . consentHandler . generateConsentStatePayloadFromMappings (
1310
- initialConsentState ,
1322
+ updatedConsentState ,
1311
1323
common . consentMappings
1312
1324
) ;
1313
1325
1326
+ // If a default consent payload exists (as selected in the mParticle UI), set it as the default
1314
1327
if ( ! common . isEmpty ( defaultConsentPayload ) ) {
1315
- common . consentPayloadAsString = JSON . stringify (
1316
- defaultConsentPayload
1328
+ common . sendDefaultConsentPayloadToGoogle ( defaultConsentPayload ) ;
1329
+ // If a default consent payload does not exist, but the user currently has updated their consent,
1330
+ // send that as the default because a default must be sent
1331
+ } else if ( ! common . isEmpty ( updatedDefaultConsentPayload ) ) {
1332
+ common . sendDefaultConsentPayloadToGoogle (
1333
+ updatedDefaultConsentPayload
1317
1334
) ;
1318
-
1319
- gtag ( 'consent' , 'default' , defaultConsentPayload ) ;
1320
1335
}
1321
1336
1337
+ common . maybeSendConsentUpdateToGoogle ( updatedConsentState ) ;
1338
+
1322
1339
return isInitialized ;
1323
1340
} ,
1324
1341
} ;
0 commit comments