Skip to content
Merged
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 @@ -86,11 +86,9 @@ describe('Amplitude Cohorts', () => {
it('should successfully retrieve an existing audience', async () => {
const externalId = 'cohort_789'

nock('https://amplitude.com')
.get(`/api/5/cohorts/request/${externalId}`)
.reply(200, {
cohortId: externalId
})
nock('https://amplitude.com').get(`/api/5/cohorts/request/${externalId}`).reply(200, {
cohortId: externalId
})

const result = await testDestination.getAudience({
settings,
Expand All @@ -103,11 +101,9 @@ describe('Amplitude Cohorts', () => {
it('should throw error if cohort not found', async () => {
const externalId = 'nonexistent_cohort'

nock('https://amplitude.com')
.get(`/api/5/cohorts/request/${externalId}`)
.reply(200, {
cohortId: 'different_id'
})
nock('https://amplitude.com').get(`/api/5/cohorts/request/${externalId}`).reply(200, {
cohortId: 'different_id'
})

await expect(
testDestination.getAudience({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@ export function getEndpointByRegion(endpoint: keyof typeof endpoints, region?: s
return endpoints[endpoint][region as Region] ?? endpoints[endpoint]['north_america']
}

export async function createAudience(request: RequestClient, settings: Settings, name: string, id_type: IDType, owner_email?: string): Promise<string> {
const {
endpoint,
app_id,
default_owner_email
} = settings
export async function createAudience(
request: RequestClient,
settings: Settings,
name: string,
id_type: IDType,
owner_email?: string
): Promise<string> {
const { endpoint, app_id, default_owner_email } = settings

if (!name) {
throw new IntegrationError('Missing audience name value', 'MISSING_REQUIRED_FIELD', 400)
}
if(!id_type){

if (!id_type) {
throw new IntegrationError('Missing id_type value', 'MISSING_REQUIRED_FIELD', 400)
}

const url = getEndpointByRegion('cohorts_upload', endpoint)

const json: CreateAudienceJSON = {
name,
app_id,
Expand All @@ -42,25 +44,31 @@ export async function createAudience(request: RequestClient, settings: Settings,
const id = response?.data?.cohortId

if (!id) {
throw new IntegrationError('Invalid response from Amplitude Cohorts API when attempting to create new Cohort: Missing cohortId', 'INVALID_RESPONSE', 500)
throw new IntegrationError(
'Invalid response from Amplitude Cohorts API when attempting to create new Cohort: Missing cohortId',
'INVALID_RESPONSE',
500
)
}
return id
}

export async function getAudience(request: RequestClient, settings: Settings, externalId: string): Promise<void> {
const {
endpoint
} = settings
const { endpoint } = settings

const url = `${getEndpointByRegion('cohorts_get_one', endpoint)}/${externalId}`
const response = await request<CreateAudienceResponse>(url)
const id = response?.data?.cohortId

if(!id) {
throw new IntegrationError('Invalid response from Amplitude Cohorts API when attempting to get Cohort: Missing cohortId', 'INVALID_RESPONSE', 500)

if (!id) {
throw new IntegrationError(
'Invalid response from Amplitude Cohorts API when attempting to get Cohort: Missing cohortId',
'INVALID_RESPONSE',
500
)
}

if(id !== externalId) {
if (id !== externalId) {
throw new IntegrationError(`Cohort with id ${externalId} not found`, 'COHORT_NOT_FOUND', 404)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
},
secret_key: {
label: 'Secret Key',
description: 'Amplitude project secret key. You can find this key in the "General" tab of your Amplitude project.',
description:
'Amplitude project secret key. You can find this key in the "General" tab of your Amplitude project.',
type: 'password',
required: true
},
app_id: {
label: 'Amplitude App ID',
description: 'The Amplitude App ID for the cohort you want to sync to. You can find this in the "General" tab of your Amplitude project.',
description:
'The Amplitude App ID for the cohort you want to sync to. You can find this in the "General" tab of your Amplitude project.',
type: 'string',
required: true
},
default_owner_email: {
label: 'Cohort Owner Email',
description: 'The email of the user who will own the cohorts in Amplitude. This can be overriden per Audience, but if left blank, all cohorts will be owned by this user.',
description:
'The email of the user who will own the cohorts in Amplitude. This can be overriden per Audience, but if left blank, all cohorts will be owned by this user.',
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The setting description contains a misspelling: “overriden” should be “overridden”. Since this is user-facing copy, it’s worth correcting.

Suggested change
'The email of the user who will own the cohorts in Amplitude. This can be overriden per Audience, but if left blank, all cohorts will be owned by this user.',
'The email of the user who will own the cohorts in Amplitude. This can be overridden per Audience, but if left blank, all cohorts will be owned by this user.',

Copilot uses AI. Check for mistakes.
type: 'string',
required: true
},
Expand All @@ -57,27 +60,25 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
}
},
testAuthentication: (request, { settings }) => {
const {
endpoint,
default_owner_email
} = settings
const { endpoint, default_owner_email } = settings
const baseUrl = getEndpointByRegion('usersearch', endpoint)
return request(`${baseUrl}?user=${default_owner_email}`)
}
Comment on lines 62 to 66
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR’s stated purpose is adding dataProvider to Microsoft Bing CAPI, but it also includes formatting-only changes in the Amplitude Cohorts destination. To keep scope focused and reviews simpler, consider reverting these unrelated formatting changes (or moving them to a separate PR).

Copilot uses AI. Check for mistakes.
},
extendRequest({ settings }) {
const { api_key, secret_key } = settings
return {
headers: {
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(`${api_key}:${secret_key}`).toString('base64')}`
Authorization: `Basic ${Buffer.from(`${api_key}:${secret_key}`).toString('base64')}`
}
}
},
audienceFields: {
owner_email: {
label: 'Cohort Owner Email',
description: 'The email of the user who will own the cohort in Amplitude. Overrides the default Cohort Owner Email value from Settings.',
description:
'The email of the user who will own the cohort in Amplitude. Overrides the default Cohort Owner Email value from Settings.',
type: 'string',
format: 'email',
required: false
Expand All @@ -94,14 +95,15 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
},
{
label: 'Amplitude ID',
value: ID_TYPES.BY_AMP_ID
value: ID_TYPES.BY_AMP_ID
}
],
default: ID_TYPES.BY_USER_ID
},
audience_name: {
label: 'Cohort Name',
description: 'The name of the cohort in Amplitude. This will override the default cohort name which is the snake_case version of the Segment Audience name.',
description:
'The name of the cohort in Amplitude. This will override the default cohort name which is the snake_case version of the Segment Audience name.',
type: 'string',
required: false
}
Expand All @@ -112,14 +114,10 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
full_audience_sync: false
},
async createAudience(request, createAudienceInput) {
const {
audienceName,
settings,
audienceSettings: {
owner_email,
audience_name,
id_type
} = {}
const {
audienceName,
settings,
audienceSettings: { owner_email, audience_name, id_type } = {}
} = createAudienceInput

const name = typeof audience_name === 'string' && audience_name.length > 0 ? audience_name : audienceName
Expand All @@ -128,13 +126,10 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
return { externalId }
},
async getAudience(request, createAudienceInput) {
const {
externalId,
settings
} = createAudienceInput
const { externalId, settings } = createAudienceInput

await getAudience(request, settings, externalId)

return { externalId }
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,46 @@ describe('Microsoft Bing CAPI (Actions) - sendEvent (updated)', () => {
expect(scope.isDone()).toBe(true)
})

test('dataProvider defaults to SEGMENT when not provided in mapping', async () => {
const event = buildTrackEvent()
const scope = nock('https://capi.uet.microsoft.com')
.post(`/v1/${settings.UetTag}/events`, (body: any) => {
expect(body.data[0].dataProvider).toBe('SEGMENT')
return true
})
.reply(200, {})
await testDestination.testAction('sendEvent', {
event,
settings,
mapping: {
data: { eventType: 'custom', eventTime: '2024-01-01T00:00:00.000Z' },
userData: { anonymousId: 'anon-1' },
timestamp: { '@path': '$.timestamp' }
}
})
expect(scope.isDone()).toBe(true)
})

test('dataProvider can be overridden via mapping', async () => {
const event = buildTrackEvent()
const scope = nock('https://capi.uet.microsoft.com')
.post(`/v1/${settings.UetTag}/events`, (body: any) => {
expect(body.data[0].dataProvider).toBe('CUSTOM_PROVIDER')
return true
})
.reply(200, {})
await testDestination.testAction('sendEvent', {
event,
settings,
mapping: {
data: { eventType: 'custom', eventTime: '2024-01-01T00:00:00.000Z', dataProvider: 'CUSTOM_PROVIDER' },
userData: { anonymousId: 'anon-1' },
timestamp: { '@path': '$.timestamp' }
}
})
expect(scope.isDone()).toBe(true)
})

test('extra data fields (eventId, pageLoadId, referrerUrl, pageTitle, keywords) are forwarded to the request', async () => {
const event = buildTrackEvent()
const scope = nock('https://capi.uet.microsoft.com')
Expand Down Expand Up @@ -291,6 +331,32 @@ describe('Microsoft Bing CAPI (Actions) - sendEvent (updated)', () => {
expect(scope.isDone()).toBe(true)
})

test('dataProvider is included in batch payloads', async () => {
const events = [buildTrackEvent({ messageId: 'm1' }), buildTrackEvent({ messageId: 'm2' })]
const scope = nock('https://capi.uet.microsoft.com')
.post(`/v1/${settings.UetTag}/events`, (body: any) => {
expect(body.data).toHaveLength(2)
expect(body.data[0].dataProvider).toBe('SEGMENT')
expect(body.data[1].dataProvider).toBe('SEGMENT')
return true
})
.reply(200, {})
const responses: any = await testDestination.executeBatch('sendEvent', {
events,
settings,
mapping: {
enable_batching: true,
data: { eventType: 'custom', eventTime: '2024-01-01T00:00:00.000Z' },
userData: { anonymousId: 'anon-1' },
timestamp: { '@path': '$.timestamp' }
}
})
expect(responses.length).toBe(2)
expect(responses[0].status).toBe(200)
expect(responses[1].status).toBe(200)
expect(scope.isDone()).toBe(true)
})

test('batch forwards extra data fields for each payload', async () => {
const events = [buildTrackEvent({ messageId: 'm1' }), buildTrackEvent({ messageId: 'm2' })]
const scope = nock('https://capi.uet.microsoft.com')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ export const data: InputField = {
],
default: 'G',
required: false
},
dataProvider: {
label: 'Data Provider',
description: 'The source of the event data.',
type: 'string',
default: 'SEGMENT',
required: false
}
},
default: {
Expand Down

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
Expand Up @@ -33,7 +33,7 @@ async function send(request: RequestClient, payloads: Payload[], settings: Setti

payloads.forEach((payload) => {
const {
data: { eventTime, eventType, adStorageConsent, eventSourceUrl, eventName, ...restOfData } = {},
data: { eventTime, eventType, adStorageConsent, eventSourceUrl, eventName, dataProvider, ...restOfData } = {},
userData: { em, ph, ...restOfUserData } = {},
Comment thread
AnkitSegment marked this conversation as resolved.
customData,
items,
Expand All @@ -46,6 +46,7 @@ async function send(request: RequestClient, payloads: Payload[], settings: Setti
adStorageConsent: adStorageConsent ?? settings.adStorageConsent,
eventSourceUrl: eventSourceUrl,
eventName: eventName,
dataProvider: dataProvider ?? 'SEGMENT',
userData: {
Comment thread
AnkitSegment marked this conversation as resolved.
...restOfUserData,
em: em ? processHashing(em, 'sha256', 'hex', (v) => v.trim().toLowerCase()) : null,
Expand All @@ -58,7 +59,6 @@ async function send(request: RequestClient, payloads: Payload[], settings: Setti
},
continueOnValidationError: true
}

json.push(jsonItem)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ export interface BingCAPIRequestItem {
eventId?: string
eventName?: string
pageLoadId?: string
referrerUr?: string
referrerUrl?: string
pageTitle?: string
keywords?: string
adStorageConsent?: string
dataProvider?: string
customData?: {
eventCategory?: string
eventLabel?: string
Expand Down
Loading