Created
October 13, 2021 22:06
-
-
Save mbaersch/e94e2499ea5bb761bf81878d815fd890 to your computer and use it in GitHub Desktop.
fix to fulfil requests for ec.js from a server-side GTM endpoint (Custom Client Template)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
___INFO___ | |
{ | |
"type": "CLIENT", | |
"id": "cvt_temp_public_id", | |
"version": 1, | |
"securityGroups": [], | |
"displayName": "Fix ec.js", | |
"brand": { | |
"id": "brand_dummy", | |
"displayName": "" | |
}, | |
"description": "send (cached) ec.js via SSGTM", | |
"containerContexts": [ | |
"SERVER" | |
] | |
} | |
___TEMPLATE_PARAMETERS___ | |
[] | |
___SANDBOXED_JS_FOR_SERVER___ | |
const setResponseBody = require('setResponseBody'); | |
const setResponseHeader = require('setResponseHeader'); | |
const setResponseStatus = require('setResponseStatus'); | |
const sendHttpGet = require('sendHttpGet'); | |
const templateDataStorage = require('templateDataStorage'); | |
const returnResponse = require('returnResponse'); | |
const key = "ec.js"; | |
if (require('getRequestPath')() === '/plugins/ua/ec.js') { | |
require('claimRequest')(); | |
setResponseHeader('content-type', 'application/javascript'); | |
const cachedBody = templateDataStorage.getItemCopy(key); | |
if (cachedBody) { | |
setResponseBody(cachedBody); | |
setResponseStatus(200); | |
returnResponse(); | |
} else { | |
sendHttpGet('https://www.google-analytics.com/plugins/ua/ec.js', (statusCode, headers, body) => { | |
templateDataStorage.setItemCopy(key, body); | |
setResponseBody(body); | |
setResponseStatus(statusCode); | |
returnResponse(); | |
}); | |
} | |
} | |
___SERVER_PERMISSIONS___ | |
[ | |
{ | |
"instance": { | |
"key": { | |
"publicId": "read_request", | |
"versionId": "1" | |
}, | |
"param": [ | |
{ | |
"key": "pathAllowed", | |
"value": { | |
"type": 8, | |
"boolean": true | |
} | |
}, | |
{ | |
"key": "requestAccess", | |
"value": { | |
"type": 1, | |
"string": "specific" | |
} | |
}, | |
{ | |
"key": "headerAccess", | |
"value": { | |
"type": 1, | |
"string": "any" | |
} | |
}, | |
{ | |
"key": "queryParameterAccess", | |
"value": { | |
"type": 1, | |
"string": "any" | |
} | |
} | |
] | |
}, | |
"clientAnnotations": { | |
"isEditedByUser": true | |
}, | |
"isRequired": true | |
}, | |
{ | |
"instance": { | |
"key": { | |
"publicId": "return_response", | |
"versionId": "1" | |
}, | |
"param": [] | |
}, | |
"isRequired": true | |
}, | |
{ | |
"instance": { | |
"key": { | |
"publicId": "access_template_storage", | |
"versionId": "1" | |
}, | |
"param": [] | |
}, | |
"isRequired": true | |
}, | |
{ | |
"instance": { | |
"key": { | |
"publicId": "access_response", | |
"versionId": "1" | |
}, | |
"param": [ | |
{ | |
"key": "writeResponseAccess", | |
"value": { | |
"type": 1, | |
"string": "any" | |
} | |
}, | |
{ | |
"key": "writeHeaderAccess", | |
"value": { | |
"type": 1, | |
"string": "specific" | |
} | |
} | |
] | |
}, | |
"clientAnnotations": { | |
"isEditedByUser": true | |
}, | |
"isRequired": true | |
}, | |
{ | |
"instance": { | |
"key": { | |
"publicId": "send_http", | |
"versionId": "1" | |
}, | |
"param": [ | |
{ | |
"key": "allowedUrls", | |
"value": { | |
"type": 1, | |
"string": "specific" | |
} | |
}, | |
{ | |
"key": "urls", | |
"value": { | |
"type": 2, | |
"listItem": [ | |
{ | |
"type": 1, | |
"string": "https://www.google-analytics.com/plugins/ua/ec.js" | |
} | |
] | |
} | |
} | |
] | |
}, | |
"clientAnnotations": { | |
"isEditedByUser": true | |
}, | |
"isRequired": true | |
} | |
] | |
___TESTS___ | |
scenarios: [] | |
___NOTES___ | |
Created on 14.10.2021, 00:03:14 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment