Skip to content

Commit

Permalink
Dynamic Yield Destination - fix audience Id (segmentio#2123)
Browse files Browse the repository at this point in the history
* adding logging

* removing logging

* refactor

* making some fields visible
  • Loading branch information
joe-ayoub-segment authored Jul 2, 2024
1 parent 2d8f0e5 commit 50108aa
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,47 +110,40 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
throw new IntegrationError('Missing computation parameters: Id and Key', 'MISSING_REQUIRED_FIELD', 400)
}

const audience_id = personas.computation_id
const audience_id = hashAndEncodeToInt(personas.computation_id)

try {
const response = await request(getCreateAudienceURL(settings.dataCenter), {
method: 'POST',
json: {
type: 'audience_subscription_request',
id: uuidv4(),
timestamp_ms: new Date().getTime(),
account: {
account_settings: {
section_id: settings.sectionId,
api_key: settings.accessKey
}
},
audience_id: hashAndEncodeToInt(audience_id),
audience_name: audience_name,
action: 'add'
const json = {
type: 'audience_subscription_request',
id: uuidv4(),
timestamp_ms: new Date().getTime(),
account: {
account_settings: {
section_id: settings.sectionId,
api_key: settings.accessKey
}
})
const responseData = await response.json()
},
audience_id: audience_id, // must be sent as an integer
audience_name: audience_name,
action: 'add'
}

if (!responseData.id) {
throw new IntegrationError(
`Failed to create Audience in Dynamic Yield - responseData.id null or undefined`,
'DYNAMIC_YIELD_AUDIENCE_CREATION_FAILED',
400
)
}
const response = await request(getCreateAudienceURL(settings.dataCenter), {
method: 'POST',
json
})
const responseData = await response.json()

return {
externalId: String(responseData.id)
}
} catch (e) {
const errorMessage = e instanceof Error ? e.message : 'Unknown error'
if (!responseData.id) {
throw new IntegrationError(
`Failed to create Audience in Dynamic Yield - ${errorMessage}`,
`Failed to create Audience in Dynamic Yield - responseData.id null or undefined`,
'DYNAMIC_YIELD_AUDIENCE_CREATION_FAILED',
400
)
}

return {
externalId: String(audience_id) // must be returned as a string
}
},
async getAudience(_, getAudienceInput) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
label: 'External Audience ID',
description: 'Unique Audience Identifier returned by the createAudience() function call.',
required: true,
unsafe_hidden: true,
unsafe_hidden: false,
default: {
'@path': '$.context.personas.external_audience_id'
}
Expand All @@ -42,7 +42,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
label: 'Audience Key',
description: 'Segment Audience key / name',
type: 'string',
unsafe_hidden: true,
unsafe_hidden: false,
required: true,
default: {
'@path': '$.context.personas.computation_key'
Expand All @@ -52,7 +52,7 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {
label: 'Traits or Properties',
description: 'Traits or Properties object',
type: 'object',
unsafe_hidden: true,
unsafe_hidden: false,
required: true,
default: {
'@if': {
Expand Down Expand Up @@ -148,38 +148,40 @@ const action: ActionDefinition<Settings, Payload, AudienceSettings> = {

const URL = getUpsertURL(settings.dataCenter)

const json = {
type: 'audience_membership_change_request',
id: payload.message_id,
timestamp_ms: new Date(payload.timestamp).getTime(),
account: {
account_settings: {
section_id: settings.sectionId,
identifier_type: identifierType,
accessKey: settings.accessKey
}
},
user_profiles: [
{
user_identities: [
{
type: identifierType,
encoding: identifierType === 'email' ? '"sha-256"' : 'raw',
value: identifierType === 'email' ? hashAndEncode(primaryIdentifier) : primaryIdentifier
}
],
audiences: [
{
audience_id: Number(external_audience_id), // must be sent as an integer
audience_name: audienceName,
action: audienceValue ? 'add' : 'delete'
}
]
}
]
}

return request(URL, {
method: 'post',
json: {
type: 'audience_membership_change_request',
id: payload.message_id,
timestamp_ms: new Date(payload.timestamp).getTime(),
account: {
account_settings: {
section_id: settings.sectionId,
identifier_type: identifierType,
accessKey: settings.accessKey
}
},
user_profiles: [
{
user_identities: [
{
type: identifierType,
encoding: identifierType === 'email' ? '"sha-256"' : 'raw',
value: identifierType === 'email' ? hashAndEncode(primaryIdentifier) : primaryIdentifier
}
],
audiences: [
{
audience_id: external_audience_id,
audience_name: audienceName,
action: audienceValue ? 'add' : 'delete'
}
]
}
]
}
json
})
}
}
Expand Down

0 comments on commit 50108aa

Please sign in to comment.