Skip to content

Commit

Permalink
testing optimizely audeince batch
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-ayoub-segment committed Jul 29, 2024
1 parent 1da8912 commit f3d2d22
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,177 @@ import nock from 'nock'
import { createTestEvent, createTestIntegration } from '@segment/actions-core'
import Destination from '../../index'

const testDestination = createTestIntegration(Destination)
let testDestination = createTestIntegration(Destination)

describe('OptimizelyAdvancedAudienceTargeting.syncAudience', () => {
const trackEvent = createTestEvent({
context: {
personas: {
computation_class: 'audience',
computation_key: 'some_audience_name',
computation_id: 'abc'
beforeEach((done) => {
testDestination = createTestIntegration(Destination)
nock.cleanAll()
done()
})

describe('single request', () => {
const trackEvent = createTestEvent({
context: {
personas: {
computation_class: 'audience',
computation_key: 'some_audience_name',
computation_id: 'abc'
},
traits: {
email: '[email protected]'
}
},
traits: {
email: '[email protected]'
email: '[email protected]',
some_audience_name: true
}
},
traits: {
email: '[email protected]',
some_audience_name: true
}
})
const identifyEvent = createTestEvent({
context: {
personas: {
computation_class: 'audience',
computation_key: 'some_audience_name',
computation_id: 'abc'
})
const identifyEvent = createTestEvent({
context: {
personas: {
computation_class: 'audience',
computation_key: 'some_audience_name',
computation_id: 'abc'
}
},
properties: {
audience_key: 'some_audience_name',
some_audience_name: true
}
}
})
})

it('should handle props with track', async () => {
nock('https://function.zaius.app/twilio_segment').post('/sync_audience').reply(201)
it('should handle traits with track', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201)

await expect(
testDestination.testAction('syncAudience', {
event: trackEvent,
useDefaultMappings: true
})
).resolves.not.toThrowError()
})
await expect(
testDestination.testAction('syncAudience', {
event: trackEvent,
useDefaultMappings: true
})
).resolves.not.toThrowError()
})

it('should handle props with track', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201)

await expect(
testDestination.testAction('syncAudience', {
event: identifyEvent,
useDefaultMappings: true
})
).resolves.not.toThrowError()
})

it('should handle errors response', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(400)

await expect(
testDestination.testAction('syncAudience', {
event: identifyEvent,
useDefaultMappings: true
})
).rejects.toThrowError()
})

it('should handle traits with track', async () => {
nock('https://function.zaius.app/twilio_segment').post('/sync_audience').reply(201)
it('should handle 401 response', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(401)

await expect(
testDestination.testAction('syncAudience', {
event: identifyEvent,
useDefaultMappings: true
await expect(
testDestination.testAction('syncAudience', {
event: identifyEvent,
useDefaultMappings: true
})
).rejects.toThrowError()
})
})

describe('batch request', () => {
const trackEvents = [
createTestEvent({
traits: {
email: '[email protected]',
some_audience_name: true
}
}),
createTestEvent({
traits: {
email: '[email protected]',
some_audience_name: true
}
})
]
const identifyEvents = [
createTestEvent({
context: {
personas: {
computation_class: 'audience',
computation_key: 'some_audience_name',
computation_id: 'abc'
}
},
properties: {
audience_key: 'some_audience_name',
some_audience_name: true
}
}),
createTestEvent({
context: {
personas: {
computation_class: 'audience',
computation_key: 'some_audience_name1',
computation_id: 'abc1'
}
},
properties: {
audience_key: 'some_audience_name1',
some_audience_name: true
}
})
).resolves.not.toThrowError()
]

it('should handle traits with track', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201)

await expect(
testDestination.testBatchAction('syncAudience', {
events: trackEvents,
useDefaultMappings: true
})
).resolves.not.toThrowError()
})

it('should handle props with track', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(201)

await expect(
testDestination.testBatchAction('syncAudience', {
events: identifyEvents,
useDefaultMappings: true
})
).resolves.not.toThrowError()
})

it('should handle errors response', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(400)

await expect(
testDestination.testBatchAction('syncAudience', {
events: identifyEvents,
useDefaultMappings: true
})
).rejects.toThrowError()
})

it('should handle 401 response', async () => {
nock('https://function.zaius.app/twilio_segment').post('/batch_sync_audience').reply(401)

await expect(
testDestination.testBatchAction('syncAudience', {
events: identifyEvents,
useDefaultMappings: true
})
).rejects.toThrowError()
})
})
})

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
@@ -1,29 +1,7 @@
import { ActionDefinition } from '@segment/actions-core'
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { getHost } from '../utils'

interface Data {
payload: Payload & {
context?: {
[k: string]: unknown
personas?: {
computation_key?: string
computation_class?: string
}
}
}
rawData?: {
context?: {
personas?: {
computation_key?: string
computation_class?: string
}
}
properties?: Record<string, boolean>
traits?: Record<string, boolean>
}
}
import { OptimizelyClient, Data } from './optimizely-client'

const action: ActionDefinition<Settings, Payload> = {
title: 'Sync Audience',
Expand Down Expand Up @@ -83,30 +61,30 @@ const action: ActionDefinition<Settings, Payload> = {
default: {
'@path': '$.timestamp'
}
},
enable_batching: {
label: 'Enable Batching',
description: 'Enable batching of event data to Optimizely.',
type: 'boolean',
default: true,
unsafe_hidden: true
},
batch_size: {
label: 'Batch Size',
description: 'Number of events to batch before sending to Optimizely.',
type: 'integer',
default: 100,
unsafe_hidden: true
}
},

perform: async (request, data) => {
const payload = data.payload
const settings = data.settings

const host = getHost(settings)

const d: Data = data
const audienceId = payload.segment_computation_id
const audienceName = payload.custom_audience_name
const audienceValue = d?.rawData?.properties?.[audienceName] ?? d?.rawData?.traits?.[audienceName]

return request(`${host}/sync_audience`, {
method: 'post',
json: {
audienceId: audienceId,
audienceName: audienceName,
timestamp: payload.timestamp,
subscription: audienceValue,
userId: payload.optimizelyUserId
}
})
const optimizelyClient = new OptimizelyClient(request, data as Data)
await optimizelyClient.send()
},
performBatch: async (request, data) => {
const optimizelyClient = new OptimizelyClient(request, data as Data)
await optimizelyClient.send()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Settings } from '../generated-types'
import type { Payload } from './generated-types'
import { getHost } from '../utils'
import { RequestClient } from '@segment/actions-core'

interface PropsOrTraits {
properties?: Record<string, boolean>
traits?: Record<string, boolean>
}

export interface Data {
payload: Payload | Array<Payload>
settings: Settings
rawData: PropsOrTraits | Array<PropsOrTraits>
}

export class OptimizelyClient {
request: RequestClient
payloads: Payload[]
settings: Settings
propsOrTraits: PropsOrTraits[]

constructor(request: RequestClient, data: Data) {
this.request = request
this.settings = data.settings
this.payloads = Array.isArray(data.payload) ? data.payload : [data.payload]
this.propsOrTraits = Array.isArray(data.rawData) ? data.rawData : [data.rawData]
}

async send() {
const host = getHost(this.settings)

const requestBody = this.payloads.map((payload, index) => {
return {
audienceId: payload.segment_computation_id,
audienceName: payload.custom_audience_name,
timestamp: payload.timestamp,
subscription:
this.propsOrTraits[index].properties?.[payload.custom_audience_name] ??
this.propsOrTraits[index].traits?.[payload.custom_audience_name],
userId: payload.optimizelyUserId
}
})

return this.request(`${host}/batch_sync_audience`, {
method: 'post',
json: requestBody
})
}
}

0 comments on commit f3d2d22

Please sign in to comment.