Skip to content

Commit

Permalink
Merge pull request #42 from stape-io/consent-settings-section
Browse files Browse the repository at this point in the history
consent settings section
  • Loading branch information
Bukashk0zzz authored Mar 25, 2024
2 parents 0395190 + 7f1c86a commit 214444d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"trailingComma": "none"
}
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
homepage: 'https://stape.io/'
versions:
- sha: eddabe8e81fef2235bcdd9c5d38e2a2a714ec681
changeNotes: Consent settings section.
- sha: 44b60bf1f406af83ee98144b192d43b6c052308c
changeNotes: Version update, Business messenger support
- sha: cddd6fe15a357fc2e8e7bc70d5d5b5f026c83795
Expand Down
25 changes: 19 additions & 6 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;

const eventData = getAllEventData();

if(!isConsentGivenOrNotRequired()) {
return data.gtmOnSuccess();
}

const url = eventData.page_location || getRequestHeader('referer');
const subDomainIndex = url ? computeEffectiveTldPlusOne(url).split('.').length - 1 : 1;

Expand Down Expand Up @@ -82,7 +87,7 @@ if (isLoggingEnabled) {
EventName: mappedEventData.event_name,
RequestMethod: 'POST',
RequestUrl: postUrl,
RequestBody: postBody,
RequestBody: postBody
})
);
}
Expand All @@ -93,7 +98,7 @@ const cookieOptions = {
samesite: 'Lax',
secure: true,
'max-age': 7776000, // 90 days
HttpOnly: !!data.useHttpOnlyCookie,
HttpOnly: !!data.useHttpOnlyCookie
};

if (fbc) {
Expand All @@ -115,7 +120,7 @@ sendHttpRequest(
EventName: mappedEventData.event_name,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
ResponseBody: body
})
);
}
Expand All @@ -134,6 +139,7 @@ sendHttpRequest(
if (data.useOptimisticScenario) {
data.gtmOnSuccess();
}

function getEventName(data) {
if (data.inheritEventName === 'inherit') {
let eventName = eventData.event_name;
Expand Down Expand Up @@ -164,7 +170,7 @@ function getEventName(data) {
'gtm4wp.productClickEEC': 'ViewContent',
'gtm4wp.checkoutOptionEEC': 'InitiateCheckout',
'gtm4wp.checkoutStepEEC': 'AddPaymentInfo',
'gtm4wp.orderCompletedEEC': 'Purchase',
'gtm4wp.orderCompletedEEC': 'Purchase'
};

if (!gaToFacebookEventName[eventName]) {
Expand All @@ -187,7 +193,7 @@ function mapEvent(eventData, data) {
action_source: data.actionSource || 'website',
event_time: Math.round(getTimestampMillis() / 1000),
custom_data: {},
user_data: {},
user_data: {}
};

if (mappedData.action_source === 'app') {
Expand Down Expand Up @@ -553,7 +559,7 @@ function setGtmEecCookie(userData) {
samesite: 'strict',
secure: true,
'max-age': 7776000, // 90 days
HttpOnly: true,
HttpOnly: true
});
}

Expand Down Expand Up @@ -601,6 +607,13 @@ function normalizePhoneNumber(phoneNumber) {
.join('');
}

function isConsentGivenOrNotRequired() {
if (data.adStorageConsent !== 'required') return true;
if (eventData.consent_state) return !!eventData.consent_state.ad_storage;
const xGaGcs = getRequestHeader('x-ga-gcs') || ''; // x-ga-gcs is a string like "G110"
return xGaGcs[3] === '1';
}

function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(
Expand Down
65 changes: 59 additions & 6 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,31 @@ ___TEMPLATE_PARAMETERS___
],
"help": "See \u003ca href\u003d\"https://developers.facebook.com/docs/marketing-api/conversions-api/parameters/custom-data\" target\u003d\"_blank\"\u003ethis documentation\u003c/a\u003e for more details on what data parameters you can add to the call."
},
{
"type": "GROUP",
"name": "consentSettingsGroup",
"displayName": "Consent Settings",
"groupStyle": "ZIPPY_CLOSED",
"subParams": [
{
"type": "RADIO",
"name": "adStorageConsent",
"displayName": "",
"radioItems": [
{
"value": "optional",
"displayValue": "Send data always"
},
{
"value": "required",
"displayValue": "Send data in case marketing consent given"
}
],
"simpleValueType": true,
"defaultValue": "optional"
}
]
},
{
"displayName": "Logs Settings",
"name": "logsGroup",
Expand Down Expand Up @@ -685,6 +710,11 @@ const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = isLoggingEnabled ? getRequestHeader('trace-id') : undefined;

const eventData = getAllEventData();

if(!isConsentGivenOrNotRequired()) {
return data.gtmOnSuccess();
}

const url = eventData.page_location || getRequestHeader('referer');
const subDomainIndex = url ? computeEffectiveTldPlusOne(url).split('.').length - 1 : 1;

Expand Down Expand Up @@ -741,7 +771,7 @@ if (isLoggingEnabled) {
EventName: mappedEventData.event_name,
RequestMethod: 'POST',
RequestUrl: postUrl,
RequestBody: postBody,
RequestBody: postBody
})
);
}
Expand All @@ -752,7 +782,7 @@ const cookieOptions = {
samesite: 'Lax',
secure: true,
'max-age': 7776000, // 90 days
HttpOnly: !!data.useHttpOnlyCookie,
HttpOnly: !!data.useHttpOnlyCookie
};

if (fbc) {
Expand All @@ -774,7 +804,7 @@ sendHttpRequest(
EventName: mappedEventData.event_name,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body,
ResponseBody: body
})
);
}
Expand All @@ -793,6 +823,7 @@ sendHttpRequest(
if (data.useOptimisticScenario) {
data.gtmOnSuccess();
}

function getEventName(data) {
if (data.inheritEventName === 'inherit') {
let eventName = eventData.event_name;
Expand Down Expand Up @@ -823,7 +854,7 @@ function getEventName(data) {
'gtm4wp.productClickEEC': 'ViewContent',
'gtm4wp.checkoutOptionEEC': 'InitiateCheckout',
'gtm4wp.checkoutStepEEC': 'AddPaymentInfo',
'gtm4wp.orderCompletedEEC': 'Purchase',
'gtm4wp.orderCompletedEEC': 'Purchase'
};

if (!gaToFacebookEventName[eventName]) {
Expand All @@ -846,7 +877,7 @@ function mapEvent(eventData, data) {
action_source: data.actionSource || 'website',
event_time: Math.round(getTimestampMillis() / 1000),
custom_data: {},
user_data: {},
user_data: {}
};

if (mappedData.action_source === 'app') {
Expand Down Expand Up @@ -1212,7 +1243,7 @@ function setGtmEecCookie(userData) {
samesite: 'strict',
secure: true,
'max-age': 7776000, // 90 days
HttpOnly: true,
HttpOnly: true
});
}

Expand Down Expand Up @@ -1260,6 +1291,13 @@ function normalizePhoneNumber(phoneNumber) {
.join('');
}

function isConsentGivenOrNotRequired() {
if (data.adStorageConsent !== 'required') return true;
if (eventData.consent_state) return !!eventData.consent_state.ad_storage;
const xGaGcs = getRequestHeader('x-ga-gcs') || ''; // x-ga-gcs is a string like "G110"
return xGaGcs[3] === '1';
}

function determinateIsLoggingEnabled() {
const containerVersion = getContainerVersion();
const isDebug = !!(
Expand Down Expand Up @@ -1616,6 +1654,21 @@ ___SERVER_PERMISSIONS___
"string": "referer"
}
]
},
{
"type": 3,
"mapKey": [
{
"type": 1,
"string": "headerName"
}
],
"mapValue": [
{
"type": 1,
"string": "x-ga-gcs"
}
]
}
]
}
Expand Down

0 comments on commit 214444d

Please sign in to comment.