Skip to content

Instantly share code, notes, and snippets.

@sahava
Created Sep 27, 2021
Embed
What would you like to do?
A server-side Google Tag Manager client template for detecting ad blockers
___INFO___
{
"type": "CLIENT",
"id": "cvt_temp_public_id",
"version": 1,
"securityGroups": [],
"displayName": "Detect Ad Blockers",
"brand": {
"id": "brand_dummy",
"displayName": ""
},
"description": "Use this Client to detect ad blockers and to create an Event Data object for an incoming pixel request.",
"containerContexts": [
"SERVER"
]
}
___TEMPLATE_PARAMETERS___
[
{
"type": "TEXT",
"name": "baitPath",
"displayName": "Path to ads.js",
"simpleValueType": true,
"defaultValue": "/ads-min.js",
"help": "Enter the path (e.g. \u003cstrong\u003e/ads-min.js\u003c/strong\u003e) to use as the bait request for the ad blocker.",
"valueValidators": [
{
"type": "REGEX",
"args": [
"^/.+$"
]
}
],
"alwaysInSummary": true
},
{
"type": "TEXT",
"name": "reqPath",
"displayName": "Path to pixel",
"simpleValueType": true,
"defaultValue": "/4dchk",
"alwaysInSummary": true,
"valueValidators": [
{
"type": "REGEX",
"args": [
"^/.+$"
]
}
]
}
]
___SANDBOXED_JS_FOR_SERVER___
const claimRequest = require('claimRequest');
const getRequestPath = require('getRequestPath');
const getRequestQueryParameters = require('getRequestQueryParameters');
const returnResponse = require('returnResponse');
const runContainer = require('runContainer');
const setPixelResponse = require('setPixelResponse');
const setResponseBody = require('setResponseBody');
const setResponseHeader = require('setResponseHeader');
const setResponseStatus = require('setResponseStatus');
const params = getRequestQueryParameters();
// Respond to requests for the bait file
if (getRequestPath() === data.baitPath) {
claimRequest();
setResponseHeader('content-type', 'text/javascript');
setResponseStatus(200);
setResponseBody("(function() {var e=document.createElement('div');e.id='GxsCRdhiJi';e.style.display='none';document.body.appendChild(e);})()");
returnResponse();
}
// Respond to requests for the pixel
if (getRequestPath() === data.reqPath) {
claimRequest();
const eventData = params;
eventData.event_name = eventData.event_name || 'page_view';
runContainer(eventData, () => {
setPixelResponse();
returnResponse();
});
}
___SERVER_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_request",
"versionId": "1"
},
"param": [
{
"key": "requestAccess",
"value": {
"type": 1,
"string": "any"
}
},
{
"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_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": "run_container",
"versionId": "1"
},
"param": []
},
"isRequired": true
}
]
___TESTS___
scenarios: []
___NOTES___
Created on 27/09/2021, 09:49:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment