Skip to content

Commit

Permalink
sessionId added
Browse files Browse the repository at this point in the history
  • Loading branch information
GetterGit committed Mar 30, 2024
1 parent c2900c0 commit 4b4ee9a
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions attribution.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,6 @@
}
}

// Fetch client ID
var clientId = getClientId();

// Check if the script has already been executed for the current session
var sessionDataFetched = sessionStorage.getItem("sessionDataFetched");
// Check if the script has already fetched a non-empty array for Metamask accounts
var sessionMetamaskFetched = sessionStorage.getItem("sessionMetamaskFetched");

if (sessionDataFetched && sessionMetamaskFetched) {
return;
}
switch (true) {
case sessionDataFetched && sessionMetamaskFetched:
// If session data and non-empy Metamask accounts array have already been fetched, exit the script
return;
case sessionDataFetched && !sessionMetamaskFetched:
getMetamaskWallets(clientId);
return;
}

// Function to generate a UUID
function generateUUID() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
Expand All @@ -58,14 +38,24 @@
return userUUID;
}

// Function to retrieve or generate session UUID
function getSessionUUID() {
var sessionUUID = sessionStorage.getItem("sessionUUID");
if (!sessionUUID) {
sessionUUID = generateUUID();
sessionStorage.setItem("sessionUUID", sessionUUID);
}
return sessionUUID;
}

// Function to retrieve source UTM
function getSourceUTM() {
var urlParams = new URLSearchParams(window.location.search);
return urlParams.toString();
}

// Function to fetch Metamask wallets array
async function getMetamaskWallets(clientId) {
async function getMetamaskWallets(clientId, sessionId) {
if (window.ethereum) {
try {
const accounts = await window.ethereum.request({
Expand All @@ -75,6 +65,7 @@
if (accounts.length > 0) {
console.log("Metamask Wallets:", accounts);
console.log("Client ID from within Metamask fetch:", clientId);
console.log("Session UUID from within Metamask fetch:", sessionId);
sessionStorage.setItem("sessionMetamaskFetched", true);
}

Expand Down Expand Up @@ -102,7 +93,7 @@
}

// Function to track user session and log data to console
async function trackUserSession(clientId) {
async function trackUserSession(clientId, sessionId) {
var userUUID = getUserUUID();
var sourceUTM = getSourceUTM();
var geo = await getGeoLocation();
Expand All @@ -111,7 +102,7 @@
var deviceType = /Mobi/.test(navigator.userAgent) ? "Mobile" : "Desktop";

// Fetch Metamask wallets array
getMetamaskWallets(clientId).then(() => {
getMetamaskWallets(clientId, sessionId).then(() => {
// Log collected data to console
console.log("User UUID:", userUUID);
console.log("Source UTM:", sourceUTM);
Expand All @@ -120,12 +111,35 @@
console.log("Device OS:", deviceOS);
console.log("Device Type:", deviceType);
console.log("Client ID:", clientId);
console.log("Session UUID", sessionId);

// Set a flag in sessionStorage to indicate that session data has been fetched
sessionStorage.setItem("sessionDataFetched", true);
});
}

// Fetch client ID
var clientId = getClientId();
// Fetch sessionUUID
var sessionId = getSessionUUID();

// Check if the script has already been executed for the current session
var sessionDataFetched = sessionStorage.getItem("sessionDataFetched");
// Check if the script has already fetched a non-empty array for Metamask accounts
var sessionMetamaskFetched = sessionStorage.getItem("sessionMetamaskFetched");

if (sessionDataFetched && sessionMetamaskFetched) {
return;
}
switch (true) {
case sessionDataFetched && sessionMetamaskFetched:
// If session data and non-empy Metamask accounts array have already been fetched, exit the script
return;
case sessionDataFetched && !sessionMetamaskFetched:
getMetamaskWallets(clientId, sessionId);
return;
}

// Track user session when the script is executed
trackUserSession(clientId);
trackUserSession(clientId, sessionId);
})();

0 comments on commit 4b4ee9a

Please sign in to comment.