From 34a638542475c19c2e148e031c9551ae0355811b Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Fri, 10 Jan 2025 17:44:44 +0100 Subject: [PATCH] fix: fix json parse errors --- .../template/googleconsentmodecheckdata.go | 35 +++++++++++++------ .../server/template/jsonrequestvaluedata.go | 6 +++- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/pkg/provider/googleconsent/server/template/googleconsentmodecheckdata.go b/pkg/provider/googleconsent/server/template/googleconsentmodecheckdata.go index 501e37d..e12a503 100644 --- a/pkg/provider/googleconsent/server/template/googleconsentmodecheckdata.go +++ b/pkg/provider/googleconsent/server/template/googleconsentmodecheckdata.go @@ -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": @@ -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]; diff --git a/pkg/tagmanager/server/template/jsonrequestvaluedata.go b/pkg/tagmanager/server/template/jsonrequestvaluedata.go index 2d7c513..40ea681 100644 --- a/pkg/tagmanager/server/template/jsonrequestvaluedata.go +++ b/pkg/tagmanager/server/template/jsonrequestvaluedata.go @@ -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 === '') {