Skip to content

Commit e1e585d

Browse files
authored
in cases where crypto.randomuuid is unavailable, polyfill with crypto randomvalues (#563)
1 parent f38a433 commit e1e585d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

app/code/Meta/Conversion/view/frontend/web/js/metaPixelTracker.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ define([
33
'jquery'
44
], function ($) {
55
'use strict';
6+
function generateUUID() {
7+
if(crypto.randomUUID) {
8+
return crypto.randomUUID();
9+
}
10+
// crypto.randomUUID() was added to chrome in late 2021. This is a passable polyfill.
11+
const buf = new Uint8Array(16);
12+
crypto.getRandomValues(buf);
13+
buf[6] = (buf[6] & 0x0f) | 0x40; // set version to 0100 (UUID version 4)
14+
buf[8] = (buf[8] & 0x3f) | 0x80; // set variant to 10 (RFC4122 variant)
15+
return Array.from(buf).map((b, i) => {
16+
const s = b.toString(16).padStart(2, '0');
17+
return (i === 4 || i === 6 || i === 8 || i === 10) ? '-' + s : s;
18+
}).join('');
19+
}
620

721
return function (config) {
822
var browserEventData = config.browserEventData,
9-
eventId = crypto.randomUUID();
23+
eventId = generateUUID();
1024

1125
config.payload.eventId = eventId;
1226

0 commit comments

Comments
 (0)