URL Parameters as Compact Token-Based Approach
Last updated
?OTA7c2lsdmVyO0pvaG4function toBase64UrlSafe(str) {
const utf8 = new TextEncoder().encode(str);
let binary = '';
utf8.forEach(b => binary += String.fromCharCode(b));
return btoa(binary)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
}
// create token from values
function pack(...parameters) {
return toBase64UrlSafe(parameters.join(';'));
}
// usage:
const token = pack(90, 'silver', 'John'); // => OTA7c2lsdmVyO0pvaG4
const url = `https://your-landing.com/video?${token}`;function fromBase64UrlSafe(b64url) {
const b64 = b64url.replace(/-/g, '+').replace(/_/g, '/');
const bin = atob(b64);
const bytes = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
return new TextDecoder().decode(bytes);
}
// Get the token from the URL and remove the leading "?"
const urlParams = location.search.slice(1)
// decode from base64 and split value by ";"
const [points, tier, name] = fromBase64UrlSafe(urlParams).split(';');
const data = { points, tier, name }
// => {points: 90, tier: 'silver', name: 'John'}
BlingsPlayer.create({
// ...
data
});