Skip to content

Instantly share code, notes, and snippets.

@milindmore22
Last active December 8, 2025 15:46
Show Gist options
  • Select an option

  • Save milindmore22/544134ea1939bd1703ff09e6d8832116 to your computer and use it in GitHub Desktop.

Select an option

Save milindmore22/544134ea1939bd1703ff09e6d8832116 to your computer and use it in GitHub Desktop.
The code uses GET parameters and sends conversion signal.
const sendHttpRequest = require('sendHttpRequest');
const getAllEventData = require('getAllEventData');
const makeInteger = require('makeInteger');
const makeTableMap = require('makeTableMap');
const JSON = require('JSON');
const getRequestHeader = require('getRequestHeader');
const log = require('logToConsole');
const getContainerVersion = require('getContainerVersion');
const encodeUriComponent = require('encodeUriComponent'); // Required for GET params
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const traceId = getRequestHeader('trace-id');
if( data.requestMethod === 'GET' ) {
let postHeaders = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36' };
} else {
let postHeaders = { 'Content-Type': 'application/json' };
}
let postBodyData = {};
// 1. Gather all data (Same as before)
if (data.sendAllEventData) {
postBodyData = getAllEventData();
}
if (data.headers) {
for (let key in data.headers) {
postHeaders[data.headers[key].key] = data.headers[key].value;
}
}
if (data.data) {
let postBodyCustomData = makeTableMap(data.data, 'key', 'value');
for (let key in postBodyCustomData) {
postBodyData[key] = postBodyCustomData[key];
}
}
// 2. Determine if we are building a Body (POST) or a Query String (GET)
let url = data.url;
let postBody; // undefined by default
if (data.requestMethod === 'GET') {
// --- GET LOGIC ---
// Convert object to query string: key=value&key2=value2
const params = [];
for (let key in postBodyData) {
// Ensure keys and values are URL safe
const encodedKey = encodeUriComponent(key);
const encodedValue = encodeUriComponent(postBodyData[key]);
params.push(encodedKey + '=' + encodedValue);
}
if (params.length) {
// Check if URL already has query params to decide between '?' and '&'
const separator = url.indexOf('?') > -1 ? '&' : '?';
url += separator + params.join('&');
}
// For GET, the body usually remains undefined or null
postBody = undefined;
} else {
// --- POST/PUT LOGIC ---
// Existing logic: Stringify to JSON
postBody = JSON.stringify(postBodyData);
}
let requestOptions = { headers: postHeaders, method: data.requestMethod };
if (data.requestTimeout) {
requestOptions.timeout = makeInteger(data.requestTimeout);
}
if (isDebug) {
log(JSON.stringify({
'Name': 'JSON Request',
'Type': 'Request',
'TraceId': traceId,
'RequestMethod': data.requestMethod,
'RequestUrl': url, // Log the updated URL
'RequestBody': postBodyData,
'RequestHeaders': postHeaders,
}));
}
if (postBodyData.action === 'conversion') {
let tempUrl = url;
// modify queryparam ?action=conversion to action=pageview
url = url.replace('action=conversion', 'action=pageview');
// remove queryparam ?data from url
url = removeParam(url, 'data');
log(url);
// send pageview first
sendHttpRequest(url, (statusCode, headers, body) => {
if (isDebug) {
log(JSON.stringify({
'Name': 'JSON Request',
'Type': 'Response',
'TraceId': traceId,
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, requestOptions, postBody);
// reset url to conversion
url = tempUrl;
// update rand param to avoid caching
url = removeParam(url, 'rand');
const randValue = postBodyData.rand + 1;
url +='&rand=' + randValue;
log(url);
// 4. send conversion request
sendHttpRequest(url, (statusCode, headers, body) => {
if (isDebug) {
log(JSON.stringify({
'Name': 'JSON Request',
'Type': 'Response',
'TraceId': traceId,
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, requestOptions, postBody);
} else {
// 3. Send Request (Using updated 'url' and 'postBody')
sendHttpRequest(url, (statusCode, headers, body) => {
if (isDebug) {
log(JSON.stringify({
'Name': 'JSON Request',
'Type': 'Response',
'TraceId': traceId,
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, requestOptions, postBody);
}
function removeParam(url, paramToRemove) {
// 1. Split URL from Query String
var parts = url.split('?');
if (parts.length < 2) return url; // Returns original if no params found
var baseUrl = parts[0];
var queryString = parts[1];
// 2. Split Query String into individual pairs
var vars = queryString.split('&');
var newVars = [];
// 3. Loop and rebuild, excluding the target param
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
// Check if key matches (pair[0])
if (pair[0] !== paramToRemove) {
newVars.push(vars[i]);
}
}
// 4. Return reconstructed URL
// If no params left, return just base. Otherwise, join with '&'
if (newVars.length === 0) return baseUrl;
return baseUrl + '?' + newVars.join('&');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment