-1

Im trying to create my own analytics script for my website, and for this I have tried to put together a way to create userID and sessionID.

But I am not sure if I have done it correctly.

I'm trying to:

  • Check to see if user has set a userID - if not set it.
  • check to see if user has a sessionID - it not set it.
  • the userID is set to follow the user, where the user continues to hold the userID on several visits, where the sessionID is unique for each visit.
if (getCookie('yb_uid') != null) {
    if (getCookie('yb_sid') != null) {
        setCookie('yb_uid', getCookie('yb_uid'), 730);
        setSessionCookie(getCookie('yb_sid'));
    } else {
        setCookie('yb_uid', getCookie('yb_uid'), 730);
        setSessionCookie(setSID());
    }
} else {
    setCookie('yb_uid', setUID(), 730);
    setSessionCookie(setSID());
}
if (getCookie('yb_sid') == null || getCookie('yb_sid') ==
    'undefined' || !getCookie('yb_sid')) {
    setSessionCookie(setSID());
}

function setUID() {
    return "ybID#" + _setID(5) + "-" + _setID(5) + "-" + Date.now();
}

function setSessionCookie(value) {
    var now = new Date();
    var minutes = 30;
    now.setTime(now.getTime() + (minutes * 60 * 1000));
    document.cookie = "yb_sid=" + value + "; max-age=" + now.toUTCString() + "; expires=" + now.toUTCString() + "; path=/";
} /*set session cookie*/
function getCookie(name) {
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null;
} /* get cookie value */
function _setID(length) {
    var result = '';
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}

function setSID() {
    var ts = Math.round(+new Date() + Math.random() / 1000);
    return ts;
}

function setCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        var expires = "; max-age=" + date.toGMTString() + " expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + ";path=/";   
     

Maybe this has an even easier way to script it?

starball
  • 20,030
  • 7
  • 43
  • 238
  • It sounds like you are looking for code review, in which case, you should post on https://codereview.stackexchange.com/ instead of stackoverflow.com – starball Oct 02 '22 at 18:20

1 Answers1

0

Have you tried UUID https://www.npmjs.com/package/uuid for generating an user id?

Wenbo
  • 1,212
  • 8
  • 17
  • what is that? Never heard of – Krister Ross Nov 24 '19 at 23:45
  • A package which is for generating UUID. Like ``` const uuidv1 = require('uuid/v1'); uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d' ``` – Wenbo Nov 25 '19 at 00:29
  • I would like to avoid using third party solutions as this would have impact on file loading time and execution. – Krister Ross Nov 25 '19 at 08:08
  • Of course, up to you. I just recommended the way in terms of how to create an unique id. I am not going to do any ads for it. But it deserves a look. See the downloads and dependents, more persuasive. I read your code ```setUID```, ```setSID``` and ```_setID```, looks good to me. But I did not read other code. From your statement, it should work. – Wenbo Nov 25 '19 at 21:15