From 7defb7f93969ca994cd476f75d375a159f7e2cba Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 31 Oct 2019 10:35:33 +0800 Subject: [PATCH] Save user data in cookie --- assets/js/p.js | 54 ++++++++++++++++++++++++++++++++---------- assets/js/plausible.js | 54 ++++++++++++++++++++++++++++++++---------- 2 files changed, 82 insertions(+), 26 deletions(-) diff --git a/assets/js/p.js b/assets/js/p.js index a634e133f..e6ff37f5f 100644 --- a/assets/js/p.js +++ b/assets/js/p.js @@ -40,32 +40,60 @@ return window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.search; } + function getUserData() { + var oldUid = getCookie('nm_uid') + var userData = JSON.parse(getCookie('plausible_user')) + + if (userData) { + userData.new_visitor = false + userData.user_agent = decodeURIComponent(userData.user_agent) + userData.referrer = decodeURIComponent(userData.referrer) + return userData + } else if (oldUid) { + return { + uid: oldUid, + new_visitor: false, + user_agent: window.navigator.userAgent, + referrer: window.document.referrer, + screen_width: window.innerWidth + } + } else { + return { + uid: pseudoUUIDv4(), + new_visitor: true, + user_agent: window.navigator.userAgent, + referrer: window.document.referrer, + screen_width: window.innerWidth + } + } + } + + function setUserData(payload) { + setCookie('plausible_user', JSON.stringify({ + uid: payload.uid, + user_agent: encodeURIComponent(payload.user_agent), + referrer: encodeURIComponent(payload.referrer), + screen_width: payload.screen_width + })) + } + function page() { if (/localhost$/.test(window.location.hostname)) return ignore('website is running locally'); if (window.location.protocol === 'file:') return ignore('website is running locally'); if (window.document.visibilityState === 'prerender') return ignore('document is prerendering'); - var existingUid = getCookie('nm_uid'); + var payload = getUserData() + payload.url = getUrl() var request = new XMLHttpRequest(); request.open('POST', plausibleHost + '/api/page', true); request.setRequestHeader('Content-Type', 'text/plain'); - var uid = existingUid || pseudoUUIDv4() - request.send(JSON.stringify({ - url: getUrl(), - new_visitor: !existingUid, - uid: uid, - user_agent: window.navigator.userAgent, - referrer: window.document.referrer, - screen_width: window.innerWidth - })); + request.send(JSON.stringify(payload)); request.onreadystatechange = function() { if (request.readyState == XMLHttpRequest.DONE) { - if (!existingUid) { - setCookie('nm_uid', uid) - } + setUserData(payload) } } } diff --git a/assets/js/plausible.js b/assets/js/plausible.js index 804ae1ea7..e024887d6 100644 --- a/assets/js/plausible.js +++ b/assets/js/plausible.js @@ -40,32 +40,60 @@ return window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.search; } + function getUserData() { + var oldUid = getCookie('nm_uid') + var userData = JSON.parse(getCookie('plausible_user')) + + if (userData) { + userData.new_visitor = false + userData.user_agent = decodeURIComponent(userData.user_agent) + userData.referrer = decodeURIComponent(userData.referrer) + return userData + } else if (oldUid) { + return { + uid: oldUid, + new_visitor: false, + user_agent: window.navigator.userAgent, + referrer: window.document.referrer, + screen_width: window.innerWidth + } + } else { + return { + uid: pseudoUUIDv4(), + new_visitor: true, + user_agent: window.navigator.userAgent, + referrer: window.document.referrer, + screen_width: window.innerWidth + } + } + } + + function setUserData(payload) { + setCookie('plausible_user', JSON.stringify({ + uid: payload.uid, + user_agent: encodeURIComponent(payload.user_agent), + referrer: encodeURIComponent(payload.referrer), + screen_width: payload.screen_width + })) + } + function page() { if (/localhost$/.test(window.location.hostname)) return ignore('website is running locally'); if (window.location.protocol === 'file:') return ignore('website is running locally'); if (window.document.visibilityState === 'prerender') return ignore('document is prerendering'); - var existingUid = getCookie('nm_uid'); + var payload = getUserData() + payload.url = getUrl() var request = new XMLHttpRequest(); request.open('POST', plausibleHost + '/api/page', true); request.setRequestHeader('Content-Type', 'text/plain'); - var uid = existingUid || pseudoUUIDv4() - request.send(JSON.stringify({ - url: getUrl(), - new_visitor: !existingUid, - uid: uid, - user_agent: window.navigator.userAgent, - referrer: window.document.referrer, - screen_width: window.innerWidth - })); + request.send(JSON.stringify(payload)); request.onreadystatechange = function() { if (request.readyState == XMLHttpRequest.DONE) { - if (!existingUid) { - setCookie('nm_uid', uid) - } + setUserData(payload) } } }