Unique session ID

Is there any unique session ID generated when the site is loaded that’s accessible to the back-end scripts?

2 Likes

I need this ID too!!!

I have the same question, cannot find anything to this topic

You can create your own using a NPM Package called UUID that generates random UUDIs.

Just install the NPM Package and import it.

import { session } from "wix-storage"
import { v4 as uuidv4 } from 'uuid'

session.setItem("sessionId", uuidv4()) //This creates the session

$w.onReady(() => {
    let sessionId = session.getItem("sessionId") //This gets the session
    console.log(sessionId) //Just logging to check it out.
})

I found and installed the package. But I am not yet a coder, could you guide me ? How do I import?
And the code above is copied and pasted somewhere?

Hi @alexchristophe

import { v4 as uuidv4} from 'uuid'

This imports the package(uuid) to your page

session.setItem("sessionId",uuidv4()) //This creates the session

Sets your session item called “sessionId” to a unique string

$w.onReady(()=>{
let sessionId = session.getItem("sessionId")//This gets the session console.log(sessionId)//Just logging to check it out.
})

This will retrieve the sessionId from your session
and logs it in your console.

@volkaertskristof It’s very kind to reply. I am totally new to Velo and Javascript though…so I do not get it. Sorry, I know this is frustrating, newbies…

  1. So where do I type i mport { v4 asuuidv4 } from ‘uuid’ ?

  2. And then I type s ession . setItem ( “sessionId” , uuidv 4 ()) under it in the same place?

  3. And then I type this $w . onReady (()=>{ let sessionId = session . getItem ( “sessionId” ) under 1) and 2) in the same place?

And then you say it will log the session ID in the console. I do not know what the console is. How do I add this back to a collection in content manager?

NB: What I am after is a field I can add to a user table with the unique sessionID associated with that anonymous user…then I can report, filter and calculate for that individual ID…

You should copy all the code I did and paste on the page you want to manipulate this sessionId .

thanks Bruno! Thanks for the visual explanation!
Does this mean that if I want to know about that session ID on multiple pages, I need to copy this code on all the pages?
And the session will remain unique or we then have a unique session ID per page?

As soon as you setup a session ID at the session level, it is usable in the whole website and you can retrieve it using the session.getItem() method. But if a person gets a link for a page that does not go through the main page, it won’t have the session ID setup. So in that case, you would need to copy this code over to other pages.

OK - I copied the code on relevant pages under existing code, respecting an empty line in between }); and the new code. I hope this is correct!

I will submit data now. Where should I look for the session IDs? Does it get automatically added to a dataset or do I need to create one?

Sessions are created for temporary use, so they are stored at client-side (browsing cookies). They are used to remember who was using your application. If you need to store the cookie used during the application use, you will have to create a log, in a dataset or otherwise.