Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
marinhero committed Nov 7, 2023
1 parent 35cc90f commit 542057f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 31 deletions.
1 change: 1 addition & 0 deletions packages/cli/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ function setupRoutes(def: DestinationDefinition | null): void {
if (Array.isArray(eventParams.data)) {
// If no mapping or default mapping is provided, default to using the first payload across all events.
eventParams.mapping = mapping || eventParams.data[0] || {}
eventParams.audienceSettings = req.body.payload[0]?.context?.personas?.audience_settings || {}
await action.executeBatch(eventParams)
} else {
await action.execute(eventParams)
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 @@ -16,6 +16,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
external_audience_id: { ...external_audience_id }
},
perform: async (request, { payload, audienceSettings, statsContext }) => {
console.log('Adding to the audience in regular')
statsContext?.statsClient?.incr('addToAudience', 1, statsContext?.tags)

if (!audienceSettings) {
Expand All @@ -25,6 +26,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
return handleUpdate(request, audienceSettings, [payload], 'add')
},
performBatch: async (request, { payload, audienceSettings, statsContext }) => {
console.log('Adding to the audience in batch ')
statsContext?.statsClient?.incr('addToAudience.batch', 1, statsContext?.tags)

if (!audienceSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,30 @@ export const prepareOfflineDataJobCreationParams = (listType: string, listId: st
}
}

const getIdentifierKey = (key: string): string => {
let k = 'publisherProvidedId'

switch (key) {
case 'email':
k = 'hashedEmail'
break
case 'phone':
k = 'hashedPhoneNumber'
break
case 'mobile':
k = 'mobileId'
break
}

return k
}

// TODO: Implement operations for customerMatch list
export const buildAddListOperation = (payload: UpdateHandlerPayload, listType: string): ListAddOperation => {
export const buildAddListOperation = (
payload: UpdateHandlerPayload,
listType: string,
key: string
): ListAddOperation => {
let op: ListAddOperation = {
create: {
userIdentifiers: {}
Expand All @@ -46,10 +68,11 @@ export const buildAddListOperation = (payload: UpdateHandlerPayload, listType: s
switch (listType) {
case 'basicUserList':
op = op as BasicListAddTypeOperation
op.create.userIdentifiers['publisher_provided_id'] = payload?.user_identifier
op.create.userIdentifiers['publisherProvidedId'] = payload?.user_identifier
break
case 'customerMatchList':
// TODO: Implement this
// @ts-ignore TODO: Remove after testing
op.create.userIdentifiers[getIdentifierKey(key)] = payload?.user_identifier
break
default:
throw new IntegrationError('Invalid list type', 'INVALID_REQUEST_DATA', 400)
Expand All @@ -68,7 +91,7 @@ export const buildRemoveListOperation = (payload: UpdateHandlerPayload, listType
switch (listType) {
case 'basicUserList':
op = op as BasicListRemoveTypeOperation
op.remove.userIdentifiers['publisher_provided_id'] = payload.user_identifier
op.remove.userIdentifiers['publisherProvidedId'] = payload.user_identifier
break
case 'customerMatchList':
// TODO: Implement this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { InputField } from '@segment/actions-core/destination-kit/types'

export const user_identifier: InputField = {
label: 'User Identifier',
description: 'The identifier for the user to add to the audience.',
description:
'The identifier for the user to add to the audience. Can only be one of the following. Basic User Lists only support Publisher Provided ID. Customer Match Lists support all four identifiers.',
type: 'string',
required: true,
default: 'publisherProvidedId',
choices: [
{ label: 'Publisher Provided ID', value: 'publisherProvidedId' },
{ label: 'Email', value: 'email' }
]
{ label: 'Publisher Provided ID', value: '$.anonymousId' },
{ label: 'Hashed Email', value: '$.email' },
{ label: 'Hashed Phone Number', value: '$.phoneNumber' },
{ label: 'Mobile ID', value: '$.context.device' } // TODO: Fix
],
default: {
'@path': '$.anonymousId'
}
}

export const enable_batching: InputField = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import { UpdateHandlerPayload, ListAddOperation, ListRemoveOperation, ListOperat
const MULTI_STATUS_ERROR_CODES_ENABLED = true

const createOfflineDataJob = async (request: RequestClient, audienceSettings: AudienceSettings, listId: string) => {
const advertiserDataJobUrl = OFFLINE_DATA_JOB_URL.replace('advertiserID', audienceSettings?.advertiserId)
const advertiserDataJobUrl = `${OFFLINE_DATA_JOB_URL.replace('advertiserID', audienceSettings?.advertiserId)}:create`
const dataJobParams = prepareOfflineDataJobCreationParams(audienceSettings?.listType, listId)
const r = await request(`${advertiserDataJobUrl}:create`, {
const r = await request(advertiserDataJobUrl, {
method: 'POST',
json: {
dataJobParams
json: dataJobParams,
headers: {
'Content-Type': 'application/json',
// @ts-ignore -- TODO: Remove
Authorization: `Bearer ${audienceSettings?.authToken}`,
'Login-Customer-Id': `products/DISPLAY_VIDEO_ADVERTISER/customers/${audienceSettings?.advertiserId}`
}
})

Expand All @@ -23,6 +27,8 @@ const createOfflineDataJob = async (request: RequestClient, audienceSettings: Au
}

const response = await r.json()

// TODO: Consider caching this job ID for a period of time
const jobId = response.resourceName.split('/').pop()

if (!jobId) {
Expand All @@ -37,7 +43,8 @@ const populateOfflineDataJob = async (
payload: UpdateHandlerPayload[],
operation: ListOperation,
jobId: string,
listType: string
listType: string,
audienceSettings: AudienceSettings
) => {
const operations: ListAddOperation[] & ListRemoveOperation[] = []
payload.forEach((p) => {
Expand All @@ -55,6 +62,12 @@ const populateOfflineDataJob = async (
json: {
operations: operations,
enablePartialFailure: MULTI_STATUS_ERROR_CODES_ENABLED
},
headers: {
'Content-Type': 'application/json',
// @ts-ignore -- TODO: Remove
Authorization: `Bearer ${audienceSettings?.authToken}`,
'Login-Customer-Id': `products/DISPLAY_VIDEO_ADVERTISER/customers/${audienceSettings?.advertiserId}`
}
})

Expand All @@ -64,6 +77,7 @@ const populateOfflineDataJob = async (
}

const performOfflineDataJob = async (request: RequestClient, jobId: string) => {
console.log('Running Datajob', jobId)
const r = await request(`${OFFLINE_DATA_JOB_URL}/${jobId}:run`)
if (r.status !== 200) {
throw new Error(`Failed to run offline data job: ${r.text}`)
Expand All @@ -82,11 +96,17 @@ export const handleUpdate = async (
jobId = await createOfflineDataJob(request, audienceSettings, payload[0]?.external_audience_id)
} catch (error) {
// Any error here would discard the entire batch, therefore, we shall retry everything.
throw new IntegrationError('Unable to create data job', 'RETRYABLE_ERROR', 500)

if (error.response.status === 400) {
throw new IntegrationError(error.response.data.error.message, 'INTEGRATION_ERROR', 400)
}

const errorMessage = JSON.parse(error.response.content).error.message
throw new IntegrationError(errorMessage, 'RETRYABLE_ERROR', 500)
}

try {
await populateOfflineDataJob(request, payload, operation, jobId, audienceSettings.listType)
await populateOfflineDataJob(request, payload, operation, jobId, audienceSettings.listType, audienceSettings)
} catch (error) {
// Any error here would discard the entire batch, therefore, we shall retry everything.
throw new IntegrationError('Unable to populate data job', 'RETRYABLE_ERROR', 500)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export type BasicListTypeMap =
export type BasicListAddTypeOperation = {
create: {
userIdentifiers: {
publisher_provided_id: string
publisherProvidedId: string
}
}
}

export type BasicListRemoveTypeOperation = {
remove: {
userIdentifiers: {
publisher_provided_id: string
publisherProvidedId: string
}
}
}
Expand All @@ -28,12 +28,9 @@ export type CustomerMatchAddListOperation = {
create: {
userIdentifiers: {
hashedEmail?: string
addressInfo?: {
hashedFirstName: string
hashedLastName: string
countryCode: string
postalCode: string
}
hashedPhoneNumber?: string
mobileId?: string
publisherProvidedId?: string
}
}
}
Expand All @@ -42,12 +39,9 @@ export type CustomerMatchRemoveListOperation = {
remove: {
userIdentifiers: {
hashedEmail?: string
addressInfo?: {
hashedFirstName: string
hashedLastName: string
countryCode: string
postalCode: string
}
hashedPhoneNumber?: string
mobileId?: string
publisherProvidedId?: string
}
}
}
Expand Down

0 comments on commit 542057f

Please sign in to comment.