Skip to main content
Question

Execute an Identify Call in my Segment workspace from a Destination Function on the onPage event

  • October 23, 2024
  • 1 reply
  • 142 views

Forum|alt.badge.img

I do not have development resources in my company that help to create an Identify call from my website. As a temporary solution I’m planning to execute the Identify Call from a Destination Function in the same workspace on the onPage event (as I have the Segment source already configured so I’m receiving page calls from that website). Is this possible?

Thanks in advance!

1 reply

Chris Carrel
Forum|alt.badge.img+4

This is very possible.

Here is a snippet from one of my destination functions that performs this action.

call.userId = user;
call.anonymousId = anonymous;

// Learn more at https://segment.com/docs/connections/spec/track/
const endpoint = 'https://api.segment.io/v1/identify'; // replace with your endpoint
let response;
console.log(call);
try {
response = await fetch(endpoint, {
method: 'POST',
headers: {
Authorization: `Basic ${btoa(settings.writeKey + ':')}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(call)
});
} catch (error) {
// Retry on connection error
throw new RetryError(error.message);
}

if (response.status >= 500 || response.status === 429) {
// Retry on 5xx (server errors) and 429s (rate limits)
throw new RetryError(`Failed with ${response.status}`);
}