Segment is a tool many companies use to collect what customers do in their product. Connecting it to Atlas means your agents see that activity without asking the customer.
Think of it like this:
Alex signs up in your app. Segment sends an "Identify" event. Atlas creates Alex as a customer.
Alex completes a purchase. Segment sends a "Track" event. Atlas adds it to Alex's timeline.
Alex writes to support. Your agent opens the ticket and already sees who Alex is and what Alex just did.
To make this work, you create a key in Atlas and paste a ready-made script in Segment. You don't need to write code.
Copy the key, check "I have copied the key" and click "Save".
Important: Atlas shows the key only once. Keep it somewhere safe until you paste it in Segment.
Part 2: Build the destination function in Segment
In Atlas, click "Connect Segment". Your Segment workspace opens.
In Segment, go to "Connections > Catalog" and open the "Functions" tab.
Click "Create Function", select "Destination" and click "Next: Build Function".
Delete the sample code in the editor and paste the Atlas script below.
In the third line, paste your key between the quotes: const SEGMENT_TO_ATLAS_TOKEN = 'your key';
This is the Atlas script you copy. It's the same for every workspace: copy it as it is and only add your key. The script tells Segment what to do with each event: create customers (onIdentify), add events to the timeline (onTrack and onPage) and create accounts (onGroup).
// Atlas destination function for Segment // Paste your key from Atlas (App Configuration > Integrations > Segment > Segment API Keys > Add Key) const SEGMENT_TO_ATLAS_TOKEN = ''; const BASE_URL = 'https://api.atlas.so'; const API_VERSION = 'v1';
async function onIdentify(event, settings) { const endpoint = `${BASE_URL}/${API_VERSION}/customers/upsert`; let response; if (!event.userId) { throw new EventNotSupported('Mandatory parameters are missing'); } if (!SEGMENT_TO_ATLAS_TOKEN) { console.log('Bad Configuration - SEGMENT_TO_ATLAS_TOKEN - missing'); return; } let firstName; let lastName; let fullName = event.traits.name; if (fullName) { firstName = fullName.split(' ')[0]; lastName = fullName.substring(firstName.length).trim() } else { firstName = event.traits.firstName || event.traits.first_name lastName = event.traits.lastName || event.traits.last_name } const payload = { userId: event.userId, email: event.traits.email, firstName: firstName, lastName: lastName, }; if (event.traits.phone) { payload['phoneNumber'] = event.traits.phone } if (event.traits.company) { payload['account'] = { name: event.traits.company.name, website: event.traits.company.domain, externalId: event.traits.company.id, }; } try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `Bearer ${SEGMENT_TO_ATLAS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (response.status >= 300) { throw new RetryError(`Failed with ${response.status}`); } } catch (error) { throw new RetryError(error.message); } }
async function onTrack(event, settings) { const endpoint = `${BASE_URL}/${API_VERSION}/events`; let response; if (!event.userId) { throw new EventNotSupported('Mandatory parameters are missing'); } if (!SEGMENT_TO_ATLAS_TOKEN) { console.log('Bad Configuration - SEGMENT_TO_ATLAS_TOKEN - missing'); return; } const atlasEvent = { objectType: 'CUSTOMER', objectId: event.userId, name: 'Event Tracked', description: event.properties.title || '', payload: { ...event.context, ...event.properties } }; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `Bearer ${SEGMENT_TO_ATLAS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(atlasEvent) }); if (response.status >= 500 || response.status === 429) { throw new RetryError(`Failed with ${response.status}`); } } catch (error) { throw new RetryError(error.message); } }
async function onPage(event, settings) { const endpoint = `${BASE_URL}/${API_VERSION}/events`; let response; if (!event.userId) { throw new EventNotSupported('Mandatory parameters are missing'); } if (!SEGMENT_TO_ATLAS_TOKEN) { console.log('Bad Configuration - SEGMENT_TO_ATLAS_TOKEN - missing'); return; } const atlasEvent = { objectType: 'CUSTOMER', objectId: event.userId, name: 'Page Viewed', description: event.properties.title || '', payload: { ...event.context, ...event.properties } }; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `Bearer ${SEGMENT_TO_ATLAS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(atlasEvent) }); if (response.status >= 500 || response.status === 429) { throw new RetryError(`Failed with ${response.status}`); } } catch (error) { throw new RetryError(error.message); } }
async function onGroup(event, settings) { const endpoint = `${BASE_URL}/${API_VERSION}/accounts/upsert`; let response; if (!event.userId) { throw new EventNotSupported('Mandatory parameters are missing'); } if (!SEGMENT_TO_ATLAS_TOKEN) { console.log('Bad Configuration - SEGMENT_TO_ATLAS_TOKEN - missing'); return; } const payload = { externalId: event.groupId, name: event.traits.name, email: event.traits.email, }; try { response = await fetch(endpoint, { method: 'POST', headers: { Authorization: `Bearer ${SEGMENT_TO_ATLAS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (response.status >= 300) { throw new RetryError(`Failed with ${response.status}`); } } catch (error) { throw new RetryError(error.message); } }
Part 3: Test it and create the function
In the test panel, click "customize the event yourself".
Paste this sample Identify event and click "Run":
Paste this sample Track event and click "Run" again: