Skip to content

Commit

Permalink
Add UUID to events, based on Segment's messageId
Browse files Browse the repository at this point in the history
  • Loading branch information
bretthoerner committed Apr 29, 2024
1 parent 325e50a commit 9265f5d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ async function onTrack(event, settings) {
const endpoint = new URL(`${baseUrl}/capture/`)
endpoint.searchParams.set('ts', event.timestamp)

const namespace = uuidv5.uuidv5('null', 'PostHog', true)
const uuid = uuidv5.uuidv5(namespace, event.messageId)

if (event.properties && event.properties.url) {
event.properties['$current_url'] = event.properties.url
delete event.properties.url
Expand All @@ -67,6 +70,7 @@ async function onTrack(event, settings) {

const res = await fetch(endpoint, {
body: JSON.stringify({
uuid,
timestamp: event.timestamp,
event: event.event,
api_key: settings.apiKey,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"license": "MIT",
"devDependencies": {
"jest": "^29.5.0",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"uuidv5": "^1.0.0"
}
}
}
6 changes: 6 additions & 0 deletions prepare-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ set -e
cp index.js module.js

cat >> module.js <<EOF
// Segment exposes uuidv5 as an object, so it is callable at uuidv5.uuidv5, see:
// https://segment.com/docs/connections/functions/destination-functions/#runtime-and-dependencies
const __uuidv5 = require('uuidv5')
const uuidv5 = {
uuidv5: __uuidv5
}
module.exports = {
onTrack,
onIdentify,
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('Segment Integration', () => {
segment_groupId: 'GroupId1',
},
timestamp: '2015-12-12T19:11:01.249Z',
uuid: '2e1e762e-c318-5c5b-a96e-955e840a3cf2',
})
})
})
Expand All @@ -86,6 +87,7 @@ describe('Segment Integration', () => {

expect(JSON.parse(lastFetchCall[1].body)).toEqual({
timestamp: '2015-02-23T22:28:55.111Z',
uuid: '38c091ab-1857-58c9-bf58-b98a022f95dd',
event: '$identify',
api_key: 'test-key',
properties: {
Expand Down Expand Up @@ -123,6 +125,7 @@ describe('Segment Integration', () => {

expect(JSON.parse(lastFetchCall[1].body)).toEqual({
timestamp: '2015-02-23T22:28:55.111Z',
uuid: '38c091ab-1857-58c9-bf58-b98a022f95dd',
event: '$set',
api_key: 'test-key',
properties: {
Expand Down Expand Up @@ -163,6 +166,7 @@ describe('Segment Integration', () => {

expect(JSON.parse(lastFetchCall[1].body)).toEqual({
timestamp: '2015-02-23T22:28:55.111Z',
uuid: '38c091ab-1857-58c9-bf58-b98a022f95dd',
event: '$groupidentify',
api_key: 'test-key',
properties: {
Expand Down Expand Up @@ -199,6 +203,7 @@ describe('Segment Integration', () => {

expect(JSON.parse(lastFetchCall[1].body)).toEqual({
timestamp: '2015-02-23T22:28:55.111Z',
uuid: '38c091ab-1857-58c9-bf58-b98a022f95dd',
event: '$pageview',
api_key: 'test-key',
properties: {
Expand Down Expand Up @@ -228,6 +233,7 @@ describe('Segment Integration', () => {

expect(JSON.parse(lastFetchCall[1].body)).toEqual({
timestamp: '2015-02-23T22:28:55.111Z',
uuid: '38c091ab-1857-58c9-bf58-b98a022f95dd',
event: '$screen',
api_key: 'test-key',
properties: {
Expand Down Expand Up @@ -255,6 +261,7 @@ describe('Segment Integration', () => {

expect(JSON.parse(lastFetchCall[1].body)).toEqual({
timestamp: '2015-02-23T22:28:55.111Z',
uuid: '38c091ab-1857-58c9-bf58-b98a022f95dd',
event: '$create_alias',
api_key: 'test-key',
properties: {
Expand Down

0 comments on commit 9265f5d

Please sign in to comment.