Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export const destination: BrowserDestinationDefinition<Settings, Function> = {
},
domain: {
description: "A custom domain to load the Google Analytics script from. For more information see [Google's Documentation](https://developers.google.com/tag-platform/tag-manager/server-side/dependency-serving?tag=gtag&option=cdn).",
label: 'Domain',
label: 'Google Custom Domain',
type: 'string',
default: 'www.googletagmanager.com'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { AMPLITUDE_API_VERSION } from './versioning-info'
import { Region } from './types'

export const endpoints = {
usersearch: {
north_america: `https://amplitude.com/api/${AMPLITUDE_API_VERSION}/usersearch`,
europe: `https://analytics.eu.amplitude.com/api/${AMPLITUDE_API_VERSION}/usersearch`
}
}

/**
* Retrieves Amplitude API endpoints for a given region. If the region
* provided does not exist, the region defaults to 'north_america'.
*
* @param endpoint name of the API endpoint
* @param region data residency region
* @returns regional API endpoint
*/
export function getEndpointByRegion(endpoint: keyof typeof endpoints, region?: string): string {
return endpoints[endpoint][region as Region] ?? endpoints[endpoint]['north_america']
}

export default endpoints

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { AudienceDestinationDefinition, defaultValues } from '@segment/actions-core'
import type { AudienceSettings, Settings } from './generated-types'
import syncAudience from './syncAudience'
import { getEndpointByRegion } from './functions'

const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
name: 'Amplitude Cohorts',
slug: 'actions-amplitude-cohorts',
mode: 'cloud',
description: 'Sync Segment Engage audiences to Amplitude Cohorts',
authentication: {
scheme: 'custom',
fields: {
apiKey: {
label: 'API Key',
description: 'Amplitude project API key. You can find this key in the "General" tab of your Amplitude project.',
type: 'password',
required: true
},
secretKey: {
label: 'Secret Key',
description:
'Amplitude project secret key. You can find this key in the "General" tab of your Amplitude project.',
type: 'password',
required: true
},
endpoint: {
label: 'Endpoint Region',
description: 'The region to send your data.',
type: 'string',
format: 'text',
choices: [
{
label: 'North America',
value: 'north_america'
},
{
label: 'Europe',
value: 'europe'
}
],
default: 'north_america'
}
},
testAuthentication: (request, { settings }) => {
const {
apiKey,
secretKey,
endpoint
} = settings

const baseUrl = getEndpointByRegion('usersearch', endpoint)
return request(`${baseUrl}?user=testUser@example.com`, {
username: apiKey,
password: secretKey
})
}
},
audienceFields: {
cohortName: {
label: 'Cohort Name',
description: 'The name of the Amplitude Cohort to sync the Engage audience to.',
type: 'string',
required: true
}
},
audienceConfig: {
mode: {
type: 'synced',
full_audience_sync: false
},
async createAudience(_request) {

return { externalId: '' }
},
async getAudience(_request) {

return { externalId: '' }
}
},
actions: {
syncAudience
},
presets: [
{
name: 'Entities Audience Membership Changed',
partnerAction: 'syncAudience',
mapping: defaultValues(syncAudience.fields),
type: 'specificEvent',
eventSlug: 'warehouse_audience_membership_changed_identify'
},
{
name: 'Associated Entity Added',
partnerAction: 'syncAudience',
mapping: defaultValues(syncAudience.fields),
type: 'specificEvent',
eventSlug: 'warehouse_entity_added_track'
},
{
name: 'Associated Entity Removed',
partnerAction: 'syncAudience',
mapping: defaultValues(syncAudience.fields),
type: 'specificEvent',
eventSlug: 'warehouse_entity_removed_track'
},
{
name: 'Journeys Step Entered',
partnerAction: 'syncAudience',
mapping: defaultValues(syncAudience.fields),
type: 'specificEvent',
eventSlug: 'journeys_step_entered_track'
}
]
}
export default destination
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { InputField } from '@segment/actions-core'

export const fields: Record<string, InputField> = {
user_id: {
label: 'User ID',
description: 'User ID',
type: 'string',
required: true,
default: {
'@path': '$.userId'
}
},
time: {
label: 'Timestamp',
description: 'The time the event occurred, in milliseconds since epoch.',
type: 'datetime',
required: true,
default: {
'@path': '$.timestamp'
}
},
user_properties: {
label: 'User Properties',
description: 'Additional data tied to the user in Amplitude. Each distinct value will show up as a user segment on the Amplitude dashboard. Object depth may not exceed 40 layers.',
type: 'object',
additionalProperties: true,
defaultObjectUI: 'keyvalue'
},
traits_or_properties: {
label: 'Traits or Properties',
description: 'Traits or Properties object from the identify() or track() call emitted by Engage',
type: 'object',
required: true,
default: {
'@if': {
exists: { '@path': '$.traits' },
then: { '@path': '$.traits' },
else: { '@path': '$.properties' }
}
}
},
segment_computation_class: {
label: 'Segment Computation Class',
description:
"Hidden field used to verify that the payload is generated by an Engage Audience. Payloads not containing computation_class = 'audience' or 'journey_step' will be dropped before the perform() fuction call.",
type: 'string',
unsafe_hidden: true,
required: true,
default: {
'@path': '$.context.personas.computation_class'
},
choices: [
{ label: 'audience', value: 'audience' },
{ label: 'journey_step', value: 'journey_step' }
]
},
segment_audience_key: {
label: 'Segment Audience Key',
description: 'Hidden field used to determine whether to add or remove the user from the Amplitude Cohort.',
type: 'string',
unsafe_hidden: true,
required: true,
default: {
'@path': '$.context.personas.computation_key'
}
},
cohort_id: {
label: 'Cohort ID',
description: 'Hidden field containing the Cohort ID which was returned when the Amplitude Cohort was created in the Audience Settings.',
type: 'string',
unsafe_hidden: true,
required: true,
default: {
'@path': '$.context.personas.external_audience_id'
}
},
batch_size: {
label: 'Max Batch Size',
description: 'The maximum number of users to process in a single batch request.',
type: 'number',
required: true,
default: 100,
maximum: 100,
minimum: 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { RequestClient } from '@segment/actions-core'
import type { AudienceSettings, Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { UpsertToCohortJSON } from './types'

export async function send(
_request: RequestClient,
payload: Payload[],
_settings: Settings,
_isBatch: boolean,
_audienceSettings?: AudienceSettings
) {


const json: UpsertToCohortJSON[] = payload.map((p) => {
const {
user_id,
time,
user_properties
} = p

const upsertToCohortJSON = {
...(user_id ? { user_id } : {}),
event_type: '[Segment] Cohort Membership Update',
time,
...(Object.keys(user_properties || {}).length > 0 ? { user_properties } : {})
}

return upsertToCohortJSON
})
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ActionDefinition } from '@segment/actions-core'
import type { AudienceSettings, Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { fields } from './fields'
import { send } from './functions'

const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
title: 'Sync To Amplitude Cohort',
description: 'Sync Segment Audience to Amplitude Cohort',
defaultSubscription: 'type = "identify" or type = "track"',
fields,
perform: (request, { payload, settings, audienceSettings }) => {
return send(request, [payload], settings, false, audienceSettings)
},
performBatch: (request, { payload, settings, audienceSettings }) => {
return send(request, payload, settings, true, audienceSettings)
}
}

export default action
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface UpsertToCohortJSON {
event_type: string
time: string | number
user_id?: string
user_properties?: Record<string, unknown>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Region = 'north_america' | 'europe'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** AMPLITUDE_API_VERSION
* Used for deletions, httpapi, usersearch endpoints.
* API reference: https://developers.amplitude.com/docs/http-api-v2
*/
export const AMPLITUDE_API_VERSION = '2'
Loading