Quick tip: 20 Char Limit

If your friendly marketer asks you to implement purchase IDs or to serialise events, you’ll have to generate 20-character IDs that should be as unique as possible.

The straight-forward approach is to use some existing IDs that the back-end provides, but often those can be longer than 20 characters.

So what can you do?

Hash Them!

The obvious answer is to use a hash function, ideally a perfect one.

But even those hash functions return more than 20 characters, right? Even a simple SHA-1 represented in Base64 binary is longer.

The thing is, though, that With a good hash function, the unpredictability of any part of the hash is proportional to the part’s size. (see this article on stackoverflow). In other words, we can use any hash function we like and then just truncate the result to 20 characters.

Nice, hm?

For those of you who use Dynamic Tag Management (or any Tag Management System, really), you could think of loading a helper function that calculates a hash in Javascript, then truncates it.

You could use code like this, this or this, a library like CryptoJS, or roll your own. The latter was a joke, of course.

About

German expat living in Switzerland (formerly UK and France). Consultant and member of the Multi-Solutions Group at Adobe, working with the Digital Marketing Suite. Father of 4 girls.

Tagged with: , , ,
Posted in Javascript, Tips
3 comments on “Quick tip: 20 Char Limit
  1. Panagiotis says:

    Happy new year Jan! Very interesting article.
    Do you know if there is a rule of thumb or a function available to estimate under which circumstances it is possible to get a duplicate? For instance, for over 1 million hashed values there is a 1% possibility to get a duplicate value when truncating to 20 chars.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 1,398 other followers

%d bloggers like this: