23Jun

Javascript to Generate a Short Unique ID

Web analytics sometimes requires generating visit ID or visitor ID for storing in the cookies. Below is the code to generate a short enough “unqie” ID for the purpose. It needs to be under 20 characters because Adobe SiteCatalyst / Omniture would only take 20 characters long serialization string. You can control the length by changing the parameter in the slice function.

Math.random().toString(36).slice(-12);

I am planning to use it with the product code to form a unique serialization string. One thing needs to be clarified is that the string is random, but not really “unique”. However, it is close enough. The chance of having two same strings is pretty small if you keep the string length over 10 characters. Appending a timestamp to string would make it unique.

Share this Story

About chaoming.li

Technology entrepreneur, web analytics specialist, Internet enthusiasts. I also like movies, computer games, history and other interesting stuff. I was born in Guangzhou China and I live in Sydney Australia now.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

© Copyright 2014, All Rights Reserved