Skip to content

Commit

Permalink
Merge pull request #80 from foomo/fix/templates
Browse files Browse the repository at this point in the history
fix: fix json parse errors
  • Loading branch information
franklinkim authored Jan 10, 2025
2 parents aeda70e + 34a6385 commit 2416084
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ const logToConsole = require('logToConsole');
const getRequestBody = require('getRequestBody');
const getCookieValues = require('getCookieValues');
const getAllEventData = require('getAllEventData');
const getRequestHeader = require('getRequestHeader');
// --- config ---
const eventData = getAllEventData();
const consentType = data.consentType;
// --- GA4 ---
const eventData = getAllEventData();
if (eventData['x-ga-gcs'] !== undefined) {
if (eventData['x-ga-gcs']) {
const gcs = eventData['x-ga-gcs'];
switch (consentType) {
case "ad_storage":
Expand All @@ -70,22 +71,36 @@ if (eventData['x-ga-gcs'] !== undefined) {
}
}
// --- MPv2 ---
const requestBody = JSON.parse(getRequestBody());
if (requestBody._consent !== undefined) {
const consent = requestBody.consent;
if (eventData['x-ga-gcd']) {
const gcd = eventData['x-ga-gcd'];
switch (consentType) {
case "ad_storage":
return (consent.ad_storage === "GRANTED") ? 'granted' : 'denied';
return (gcd.substring(2, 3) === "1") ? 'granted' : 'denied';
case "analytics_storage":
return (consent.analytics_storage === "GRANTED") ? 'granted' : 'denied';
return (gcd.substring(3, 4) === "1") ? 'granted' : 'denied';
default:
return 'denied';
}
}
// --- MPv2 ---
let requestBody = getRequestBody();
if (requestBody && getRequestHeader('content-type') === 'application/json') {
requestBody = JSON.parse(requestBody);
if (requestBody._consent) {
const consent = requestBody.consent;
switch (consentType) {
case "ad_storage":
return (consent.ad_storage === "GRANTED") ? 'granted' : 'denied';
case "analytics_storage":
return (consent.analytics_storage === "GRANTED") ? 'granted' : 'denied';
default:
return 'denied';
}
}
}
// --- Cookiebot ---
const cookiebotCookie = getCookieValues('CookieConsent')[0];
Expand Down
6 changes: 5 additions & 1 deletion pkg/tagmanager/server/template/jsonrequestvaluedata.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ ___TEMPLATE_PARAMETERS___
___SANDBOXED_JS_FOR_SERVER___
// Enter your template code here.
const JSON = require('JSON');
const logToConsole = require('logToConsole');
const getRequestBody = require('getRequestBody');
const getRequestHeader = require('getRequestHeader');
if (getRequestHeader('content-type') !== 'application/json') {
return null;
}
const requestBody = getRequestBody();
if (requestBody === '') {
Expand Down

0 comments on commit 2416084

Please sign in to comment.