Skip to content

Commit

Permalink
Merge pull request #9 from stape-io/adding_uuid_generation
Browse files Browse the repository at this point in the history
Adding UUID generation
  • Loading branch information
Bukashk0zzz authored Nov 13, 2024
2 parents ff3985b + 48b024d commit 3ec5c01
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
homepage: "https://stape.io/"
versions:
- sha: 77f4014e15641af8677ee96b9a92b9ba42042989
changeNotes: Added generation UUID v4 for distinct_id.
- sha: 08caf7f12c53469ee358455489c3168511101c43
changeNotes: Fix the way the UTMs are being sent.
- sha: d105fb188c244d238979c69cf2e872b75da98aa5
Expand Down
35 changes: 23 additions & 12 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ 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 getTimestamp = require('getTimestamp');

const postUrl = 'https://' + (data.serverEU ? 'api-eu.mixpanel.com' : 'api.mixpanel.com');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');

const eventData = getAllEventData();


let cookieOptions = {
domain: 'auto',
path: '/',
Expand Down Expand Up @@ -47,7 +49,6 @@ if (data.type === 'track') {
return;
}


function sendAppendProfileRequest() {
const propertiesToAppend = {};
data.userPropertiesToAppend.forEach(row => {
Expand Down Expand Up @@ -102,10 +103,6 @@ function sendAppendProfileRequest() {
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}





function sendSetProfileRequest() {
const userProperties = {};
data.userPropertiesTable.forEach(row => {
Expand Down Expand Up @@ -158,8 +155,6 @@ function sendSetProfileRequest() {
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}



function sendTrackRequest() {
let postBody = {
properties: {}
Expand Down Expand Up @@ -193,7 +188,8 @@ function sendTrackRequest() {
}
});
}



if (data.trackParametersRemove) {
data.trackParametersRemove.forEach(d => {
Object.delete(postBody.properties, d.name);
Expand Down Expand Up @@ -278,7 +274,7 @@ function getDistinctId() {
let distinctIdCookie = getCookieValues('stape_mixpanel_distinct_id')[0];
if (distinctIdCookie) return distinctIdCookie;

return 's-' + getTimestampMillis() + '-' + generateRandom(100000, 999999) + '-' + generateRandom(100000, 999999) + '-' + generateRandom(100000, 999999);
return UUID();
}

function getDeviceId(distinctId) {
Expand Down Expand Up @@ -447,6 +443,21 @@ function getSearchEngine(referrer) {
return null;
}

function random() {
return generateRandom(1000000000000000, 10000000000000000)/10000000000000000;
}

function UUID() {
function s(n) { return h((random() * (1<<(n<<2)))^getTimestamp()).slice(-n); }
function h(n) { return (n|0).toString(16); }
return [
s(4) + s(4), s(4),
'4' + s(3),
h(8|(random()*4)) + s(3),
getTimestamp().toString(16).slice(-10) + s(2)
].join('-');
}

function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
Expand All @@ -461,4 +472,4 @@ function determinateIsLoggingEnabled() {
}

return data.logType === 'always';
}
}
30 changes: 20 additions & 10 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,16 @@ 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 getTimestamp = require('getTimestamp');

const postUrl = 'https://' + (data.serverEU ? 'api-eu.mixpanel.com' : 'api.mixpanel.com');
const containerVersion = getContainerVersion();
const isDebug = containerVersion.debugMode;
const isLoggingEnabled = determinateIsLoggingEnabled();
const traceId = getRequestHeader('trace-id');

const eventData = getAllEventData();


let cookieOptions = {
domain: 'auto',
path: '/',
Expand Down Expand Up @@ -421,7 +423,6 @@ if (data.type === 'track') {
return;
}


function sendAppendProfileRequest() {
const propertiesToAppend = {};
data.userPropertiesToAppend.forEach(row => {
Expand Down Expand Up @@ -476,10 +477,6 @@ function sendAppendProfileRequest() {
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}





function sendSetProfileRequest() {
const userProperties = {};
data.userPropertiesTable.forEach(row => {
Expand Down Expand Up @@ -532,8 +529,6 @@ function sendSetProfileRequest() {
}, {headers: {'Content-Type': 'application/json'}, method: 'POST'}, JSON.stringify([profileBody]));
}



function sendTrackRequest() {
let postBody = {
properties: {}
Expand Down Expand Up @@ -653,7 +648,7 @@ function getDistinctId() {
let distinctIdCookie = getCookieValues('stape_mixpanel_distinct_id')[0];
if (distinctIdCookie) return distinctIdCookie;
return 's-' + getTimestampMillis() + '-' + generateRandom(100000, 999999) + '-' + generateRandom(100000, 999999) + '-' + generateRandom(100000, 999999);
return UUID();
}

function getDeviceId(distinctId) {
Expand Down Expand Up @@ -822,6 +817,21 @@ function getSearchEngine(referrer) {
return null;
}

function random() {
return generateRandom(1000000000000000, 10000000000000000)/10000000000000000;
}

function UUID() {
function s(n) { return h((random() * (1<<(n<<2)))^getTimestamp()).slice(-n); }
function h(n) { return (n|0).toString(16); }
return [
s(4) + s(4), s(4),
'4' + s(3),
h(8|(random()*4)) + s(3),
getTimestamp().toString(16).slice(-10) + s(2)
].join('-');
}

function determinateIsLoggingEnabled() {
if (!data.logType) {
return isDebug;
Expand Down

0 comments on commit 3ec5c01

Please sign in to comment.