Skip to content

Commit

Permalink
Merge pull request #1 from mbrucki/new_request_options
Browse files Browse the repository at this point in the history
New request options added
  • Loading branch information
kHorozhanov authored Nov 27, 2023
2 parents 7c00ace + 9e57741 commit a9bb8d5
Show file tree
Hide file tree
Showing 2 changed files with 313 additions and 7 deletions.
119 changes: 117 additions & 2 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const parseUrl = require('parseUrl');
const makeString = require('makeString');
const Object = require('Object');
const makeNumber = require('makeNumber');
const postUrl = 'https://' + (data.serverEU ? 'api-eu.mixpanel.com' : 'api.mixpanel.com');

const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
Expand All @@ -34,16 +35,130 @@ if (data.type === 'track') {
sendAliasRequest();
} else if (data.type === 'identify') {
sendIdentifyRequest();
} else if (data.type === 'profile-set') {
sendSetProfileRequest();
} else if (data.type === 'profile-append') {
sendAppendProfileRequest();
} else if (data.type === 'reset') {
cookieOptions['max-age'] = 1;

setCookie('stape_mixpanel_distinct_id', 'empty', cookieOptions);
setCookie('stape_mixpanel_device_id', 'empty', cookieOptions);
data.gtmOnSuccess();

return;
}


function sendAppendProfileRequest() {
const propertiesToAppend = {};
data.userPropertiesToAppend.forEach(row => {
if (!propertiesToAppend[row.propertyName]) {
propertiesToAppend[row.propertyName] = [];
}
propertiesToAppend[row.propertyName].push(row.valueToAppend);
});

const profileBody = {
'$token': data.token,
'$distinct_id': getDistinctId(),
'$append': propertiesToAppend
};

const postUrlAppend = postUrl + '/engage#profile-list-append';

// Logging the request if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'Profile Append',
'RequestMethod': 'POST',
'RequestUrl': postUrlAppend,
'RequestBody': profileBody,
}));
}

// Sending the HTTP request to Mixpanel
sendHttpRequest(postUrlAppend, (statusCode, headers, body) => {
// Logging the response if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': 'Profile Append',
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}

// Handling the response
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}





function sendSetProfileRequest() {
const userProperties = {};
data.userPropertiesTable.forEach(row => {
userProperties[row.userProperty] = row.value;
});

const profileBody = {
'$token': data.token,
'$distinct_id': getDistinctId(),
'$set': userProperties
};

const postUrlSet = postUrl + '/engage#profile-set';

// Logging the request if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'Profile Set',
'RequestMethod': 'POST',
'RequestUrl': postUrlSet,
'RequestBody': profileBody,
}));
}

// Sending the HTTP request to Mixpanel
sendHttpRequest(postUrlSet, (statusCode, headers, body) => {
// Logging the response if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': 'Profile Set',
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}

// Handling the response
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}



function sendTrackRequest() {
let postBody = {
properties: {}
Expand Down
201 changes: 196 additions & 5 deletions template.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
___TERMS_OF_SERVICE___
___TERMS_OF_SERVICE___

By creating or modifying this file you agree to Google Tag Manager's Community
Template Gallery Developer Terms of Service available at
Expand All @@ -15,8 +15,8 @@ ___INFO___
"securityGroups": [],
"displayName": "Mixpanel",
"brand": {
"id": "brand_dummy",
"displayName": "Custom Template",
"id": "github.com_stape-io",
"displayName": "stape-io",
"thumbnail": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAAAXNSR0IArs4c6QAAAFBlWElmTU0AKgAAAAgAAgESAAMAAAABAAEAAIdpAAQAAAABAAAAJgAAAAAAA6ABAAMAAAABAAEAAKACAAQAAAABAAAAYKADAAQAAAABAAAAYAAAAAC77nFQAAABWWlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iWE1QIENvcmUgNi4wLjAiPgogICA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPgogICAgICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIgogICAgICAgICAgICB4bWxuczp0aWZmPSJodHRwOi8vbnMuYWRvYmUuY29tL3RpZmYvMS4wLyI+CiAgICAgICAgIDx0aWZmOk9yaWVudGF0aW9uPjE8L3RpZmY6T3JpZW50YXRpb24+CiAgICAgIDwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgoZXuEHAAAGvklEQVR4Ae2aW2wUVRjHvzPTQotiBVQIcscIEiBGor4RHtSQGCBAtsUbGl8AhUQSQ7k9bCKm3BIwGKKoUUHFZmm4Bg3SxMQn8UqIXEXAB1FQQEF6nT3+z3Sn3cLYnd2Z2RL4TzKZmTPn/L8zv+/cz4jwIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESIAESCEVA5ZN6SULfnRYZpSwZLVqGIG2FKNE6LRcsJWe0ksNlTXIiuUNdykf3Vo4bwAFaVVfJeEtkVlrLZCQYCWC9faBphBnwR3GzR2upW51Sx3ziMSiLQJcOWDZLD3a0LEBpfwZpBmaly32r5Bel5e1GLe+uS6kLuRPcmjH+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x4Se17EDtXAgtU0BMPs1xBTndid3Dl9ek1O8mwB0e2SKH0PufxsuuOg4TP6rjUHMPORWFGNrsfeg4TXUfE1IPc1HZmim9IaVEMsvd72HZpR6OeMibiPUU+S5Z69YQ10Zm/KzVkkp5A8VoQWjLAQQAbGFNrVofIGqgKNVVeg4+cgMilwZK4BMJIL7ArKJq5Sfqos/r2ILamiBRGluPH8HK+dgsdQgfs9Oys+Mx/F35VdmCuczmgpWwfYrvX15s+Ca/GQdgkQJVA89bC/6IYAnTKP1vrUipSJofz6TZVHGaZSmeP8SZ9sIDXs2PA/PwA8GBgPEjjdbuALfNsmQdqvK3kVroLLanoUk+6BwUzdPa7epcutFtQpdicnYmgKoZdNRZtlSurFX7AsSPJUqmD+jQjmtLEh38DzD2bDG2JLGnPQZNUiVsdtqShP0GhJ9D+AEUtLqWMqmPYkW2g17+d9c5wEhEvSkPyYOY4Mzprk15lZZ+qOpmM+iytMjZG3pT3vOh+S0FmV6DkZG7ZuGF53lFcuy72lLN31L8yfnWAC8q1q2HoNTMR5V9DmEDvPAgVwifRLxNGNq90x2jiyB5vBHidOkAk8FkUlvNR+RBDNOq0G6aNtWsl/sts5rSbtZljuLc7TiybU2dCj3bhdZNfeR0QPbXez/nYir9AGgPxrsKXM364UUMaE9jtne0V4sc58+52dR4TwIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkQAIkECuB/wBPZdJDvg0l/QAAAABJRU5ErkJggg\u003d\u003d"
},
"description": "Utilizes Mixpanel server API for user and event tracking.",
Expand Down Expand Up @@ -62,6 +62,14 @@ ___TEMPLATE_PARAMETERS___
{
"value": "reset",
"displayValue": "reset"
},
{
"value": "profile-set",
"displayValue": "set"
},
{
"value": "profile-append",
"displayValue": "append"
}
],
"simpleValueType": true,
Expand Down Expand Up @@ -102,6 +110,74 @@ ___TEMPLATE_PARAMETERS___
}
]
},
{
"type": "GROUP",
"name": "appendGroup",
"displayName": "User Properties to append",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "SIMPLE_TABLE",
"name": "userPropertiesToAppend",
"displayName": "",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "User Property",
"name": "propertyName",
"type": "TEXT"
},
{
"defaultValue": "",
"displayName": "Value",
"name": "valueToAppend",
"type": "TEXT"
}
]
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "profile-append",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "setGroup",
"displayName": "User Properties",
"groupStyle": "ZIPPY_OPEN",
"subParams": [
{
"type": "SIMPLE_TABLE",
"name": "userPropertiesTable",
"displayName": "",
"simpleTableColumns": [
{
"defaultValue": "",
"displayName": "User Property",
"name": "userProperty",
"type": "TEXT"
},
{
"defaultValue": "",
"displayName": "Value",
"name": "value",
"type": "TEXT"
}
]
}
],
"enablingConditions": [
{
"paramName": "type",
"paramValue": "profile-set",
"type": "EQUALS"
}
]
},
{
"type": "GROUP",
"name": "identifyGroup",
Expand Down Expand Up @@ -290,6 +366,7 @@ const parseUrl = require('parseUrl');
const makeString = require('makeString');
const Object = require('Object');
const makeNumber = require('makeNumber');
const postUrl = 'https://' + (data.serverEU ? 'api-eu.mixpanel.com' : 'api.mixpanel.com');

const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
Expand All @@ -312,16 +389,130 @@ if (data.type === 'track') {
sendAliasRequest();
} else if (data.type === 'identify') {
sendIdentifyRequest();
} else if (data.type === 'profile-set') {
sendSetProfileRequest();
} else if (data.type === 'profile-append') {
sendAppendProfileRequest();
} else if (data.type === 'reset') {
cookieOptions['max-age'] = 1;
setCookie('stape_mixpanel_distinct_id', 'empty', cookieOptions);
setCookie('stape_mixpanel_device_id', 'empty', cookieOptions);
data.gtmOnSuccess();
return;
}


function sendAppendProfileRequest() {
const propertiesToAppend = {};
data.userPropertiesToAppend.forEach(row => {
if (!propertiesToAppend[row.propertyName]) {
propertiesToAppend[row.propertyName] = [];
}
propertiesToAppend[row.propertyName].push(row.valueToAppend);
});

const profileBody = {
'$token': data.token,
'$distinct_id': getDistinctId(),
'$append': propertiesToAppend
};

const postUrlAppend = postUrl + '/engage#profile-list-append';

// Logging the request if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'Profile Append',
'RequestMethod': 'POST',
'RequestUrl': postUrlAppend,
'RequestBody': profileBody,
}));
}

// Sending the HTTP request to Mixpanel
sendHttpRequest(postUrlAppend, (statusCode, headers, body) => {
// Logging the response if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': 'Profile Append',
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}

// Handling the response
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}





function sendSetProfileRequest() {
const userProperties = {};
data.userPropertiesTable.forEach(row => {
userProperties[row.userProperty] = row.value;
});

const profileBody = {
'$token': data.token,
'$distinct_id': getDistinctId(),
'$set': userProperties
};

const postUrlSet = postUrl + '/engage#profile-set';

// Logging the request if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Request',
'TraceId': traceId,
'EventName': 'Profile Set',
'RequestMethod': 'POST',
'RequestUrl': postUrlSet,
'RequestBody': profileBody,
}));
}

// Sending the HTTP request to Mixpanel
sendHttpRequest(postUrlSet, (statusCode, headers, body) => {
// Logging the response if logging is enabled
if (isLoggingEnabled) {
logToConsole(JSON.stringify({
'Name': 'Mixpanel',
'Type': 'Response',
'TraceId': traceId,
'EventName': 'Profile Set',
'ResponseStatusCode': statusCode,
'ResponseHeaders': headers,
'ResponseBody': body,
}));
}

// Handling the response
if (statusCode >= 200 && statusCode < 400) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}



function sendTrackRequest() {
let postBody = {
properties: {}
Expand Down

0 comments on commit a9bb8d5

Please sign in to comment.