Skip to content

Commit 8d0c746

Browse files
monutwilionick-Ag
authored andcommitted
added context.personas.computation_key for update audience (#3065)
* added context.personas.computation_key for update audience * add '$.context.personas.computation_key' dmp_segment_name * added tests * added tests for dmp_segment_name
1 parent 6b92809 commit 8d0c746

File tree

2 files changed

+237
-3
lines changed
  • packages/destination-actions/src/destinations/linkedin-audiences/updateAudience

2 files changed

+237
-3
lines changed

packages/destination-actions/src/destinations/linkedin-audiences/updateAudience/__tests__/index.test.ts

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,232 @@ describe('LinkedinAudiences.updateAudience', () => {
401401
'"{\\"elements\\":[{\\"action\\":\\"ADD\\",\\"userIds\\":[{\\"idType\\":\\"SHA256_EMAIL\\",\\"idValue\\":\\"584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777\\"},{\\"idType\\":\\"GOOGLE_AID\\",\\"idValue\\":\\"123\\"}],\\"firstName\\":\\"John\\",\\"lastName\\":\\"Doe\\",\\"title\\":\\"CEO\\",\\"company\\":\\"Acme\\",\\"country\\":\\"US\\"}]}"'
402402
)
403403
})
404+
405+
it('should use context.personas.computation_key as source_segment_id when properties.audience_key does not exist', async () => {
406+
const eventWithComputationKey = createTestEvent({
407+
event: 'Audience Entered',
408+
type: 'track',
409+
properties: {
410+
// No audience_key property
411+
},
412+
context: {
413+
personas: {
414+
computation_key: 'from_computation_key' // gitleaks:allow
415+
},
416+
traits: {
417+
418+
},
419+
device: {
420+
advertisingId: '123'
421+
}
422+
}
423+
})
424+
425+
const expectedUrlParams = {
426+
q: 'account',
427+
account: 'urn:li:sponsoredAccount:123',
428+
sourceSegmentId: 'from_computation_key', // gitleaks:allow
429+
sourcePlatform: LINKEDIN_SOURCE_PLATFORM
430+
}
431+
432+
nock(`${BASE_URL}/dmpSegments`)
433+
.get(/.*/)
434+
.query(expectedUrlParams)
435+
.reply(200, { elements: [{ id: 'dmp_segment_id' }] })
436+
nock(`${BASE_URL}/dmpSegments/dmp_segment_id/users`).post(/.*/).reply(200)
437+
438+
await expect(
439+
testDestination.testAction('updateAudience', {
440+
event: eventWithComputationKey,
441+
settings: {
442+
ad_account_id: '123',
443+
send_email: true,
444+
send_google_advertising_id: true
445+
},
446+
useDefaultMappings: true,
447+
auth,
448+
mapping: {
449+
personas_audience_key: 'from_computation_key' // gitleaks:allow
450+
}
451+
})
452+
).resolves.not.toThrowError()
453+
})
454+
455+
it('should prioritize properties.audience_key over context.personas.computation_key when both exist', async () => {
456+
const eventWithBothKeys = createTestEvent({
457+
event: 'Audience Entered',
458+
type: 'track',
459+
properties: {
460+
audience_key: 'from_properties_audience_key' // gitleaks:allow
461+
},
462+
context: {
463+
personas: {
464+
computation_key: 'from_computation_key' // gitleaks:allow
465+
},
466+
traits: {
467+
468+
},
469+
device: {
470+
advertisingId: '123'
471+
}
472+
}
473+
})
474+
475+
const expectedUrlParams = {
476+
q: 'account',
477+
account: 'urn:li:sponsoredAccount:123',
478+
sourceSegmentId: 'from_properties_audience_key', // Should use this, not computation_key // gitleaks:allow
479+
sourcePlatform: LINKEDIN_SOURCE_PLATFORM
480+
}
481+
482+
nock(`${BASE_URL}/dmpSegments`)
483+
.get(/.*/)
484+
.query(expectedUrlParams)
485+
.reply(200, { elements: [{ id: 'dmp_segment_id' }] })
486+
nock(`${BASE_URL}/dmpSegments/dmp_segment_id/users`).post(/.*/).reply(200)
487+
488+
await expect(
489+
testDestination.testAction('updateAudience', {
490+
event: eventWithBothKeys,
491+
settings: {
492+
ad_account_id: '123',
493+
send_email: true,
494+
send_google_advertising_id: true
495+
},
496+
useDefaultMappings: true,
497+
auth,
498+
mapping: {
499+
personas_audience_key: 'from_properties_audience_key' // gitleaks:allow
500+
}
501+
})
502+
).resolves.not.toThrowError()
503+
})
504+
505+
it('should use context.personas.computation_key as dmp_segment_name when properties.audience_key does not exist', async () => {
506+
const eventWithComputationKey = createTestEvent({
507+
event: 'Audience Entered',
508+
type: 'track',
509+
properties: {
510+
// No audience_key property
511+
},
512+
context: {
513+
personas: {
514+
computation_key: 'from_computation_key' // gitleaks:allow
515+
},
516+
traits: {
517+
518+
},
519+
device: {
520+
advertisingId: '123'
521+
}
522+
}
523+
})
524+
525+
const expectedCreateDmpSegmentRequestBody = {
526+
name: 'from_computation_key', // gitleaks:allow
527+
sourcePlatform: LINKEDIN_SOURCE_PLATFORM,
528+
sourceSegmentId: 'from_computation_key', // gitleaks:allow
529+
account: `urn:li:sponsoredAccount:123`,
530+
type: 'USER',
531+
destinations: [
532+
{
533+
destination: 'LINKEDIN'
534+
}
535+
]
536+
}
537+
538+
nock(`${BASE_URL}/dmpSegments`)
539+
.get(/.*/)
540+
.query(() => true)
541+
.reply(200, { elements: [] })
542+
nock(`${BASE_URL}/dmpSegments`)
543+
.post(/.*/, expectedCreateDmpSegmentRequestBody)
544+
.reply(200, {}, { 'x-linkedin-id': 'new_dmp_segment_id' })
545+
nock(`${BASE_URL}/dmpSegments`)
546+
.get(/.*/)
547+
.query(() => true)
548+
.reply(200, { elements: [{ id: 'new_dmp_segment_id' }] })
549+
nock(`${BASE_URL}/dmpSegments/new_dmp_segment_id/users`).post(/.*/).reply(200)
550+
551+
await expect(
552+
testDestination.testAction('updateAudience', {
553+
event: eventWithComputationKey,
554+
settings: {
555+
ad_account_id: '123',
556+
send_email: true,
557+
send_google_advertising_id: true
558+
},
559+
useDefaultMappings: true,
560+
auth,
561+
mapping: {
562+
personas_audience_key: 'from_computation_key' // gitleaks:allow
563+
}
564+
})
565+
).resolves.not.toThrowError()
566+
})
567+
568+
it('should prioritize properties.audience_key over context.personas.computation_key for dmp_segment_name when both exist', async () => {
569+
const eventWithBothKeys = createTestEvent({
570+
event: 'Audience Entered',
571+
type: 'track',
572+
properties: {
573+
audience_key: 'from_properties_audience_key' // gitleaks:allow
574+
},
575+
context: {
576+
personas: {
577+
computation_key: 'from_computation_key' // gitleaks:allow
578+
},
579+
traits: {
580+
581+
},
582+
device: {
583+
advertisingId: '123'
584+
}
585+
}
586+
})
587+
588+
const expectedCreateDmpSegmentRequestBody = {
589+
name: 'from_properties_audience_key', // Should use this, not computation_key // gitleaks:allow
590+
sourcePlatform: LINKEDIN_SOURCE_PLATFORM,
591+
sourceSegmentId: 'from_properties_audience_key', // gitleaks:allow
592+
account: `urn:li:sponsoredAccount:123`,
593+
type: 'USER',
594+
destinations: [
595+
{
596+
destination: 'LINKEDIN'
597+
}
598+
]
599+
}
600+
601+
nock(`${BASE_URL}/dmpSegments`)
602+
.get(/.*/)
603+
.query(() => true)
604+
.reply(200, { elements: [] })
605+
nock(`${BASE_URL}/dmpSegments`)
606+
.post(/.*/, expectedCreateDmpSegmentRequestBody)
607+
.reply(200, {}, { 'x-linkedin-id': 'new_dmp_segment_id' })
608+
nock(`${BASE_URL}/dmpSegments`)
609+
.get(/.*/)
610+
.query(() => true)
611+
.reply(200, { elements: [{ id: 'new_dmp_segment_id' }] })
612+
nock(`${BASE_URL}/dmpSegments/new_dmp_segment_id/users`).post(/.*/).reply(200)
613+
614+
await expect(
615+
testDestination.testAction('updateAudience', {
616+
event: eventWithBothKeys,
617+
settings: {
618+
ad_account_id: '123',
619+
send_email: true,
620+
send_google_advertising_id: true
621+
},
622+
useDefaultMappings: true,
623+
auth,
624+
mapping: {
625+
personas_audience_key: 'from_properties_audience_key' // gitleaks:allow
626+
}
627+
})
628+
).resolves.not.toThrowError()
629+
})
404630
})
405631

406632
describe('Error cases', () => {

packages/destination-actions/src/destinations/linkedin-audiences/updateAudience/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const action: ActionDefinition<Settings, Payload> = {
1717
'The display name of the LinkedIn DMP Segment. This field is set only when Segment creates a new audience. Updating this field after Segment has created an audience will not update the audience name in LinkedIn.',
1818
type: 'string',
1919
default: {
20-
'@path': '$.properties.audience_key'
20+
'@if': {
21+
exists: { '@path': '$.properties.audience_key' },
22+
then: { '@path': '$.properties.audience_key' },
23+
else: { '@path': '$.context.personas.computation_key' }
24+
}
2125
}
2226
},
2327
enable_batching: {
@@ -94,9 +98,13 @@ const action: ActionDefinition<Settings, Payload> = {
9498
description:
9599
"A Segment-specific key associated with the LinkedIn DMP Segment. This is the lookup key Segment uses to fetch the DMP Segment from LinkedIn's API.",
96100
type: 'string',
97-
unsafe_hidden: true, // This field is hidden from customers because the desired value always appears at '$.properties.audience_key' in Personas events.
101+
unsafe_hidden: true, // This field is hidden from customers because the desired value always appears at '$.properties.audience_key' in Personas events and at '$.context.personas.computation_key' for events coming from Journeys v2.
98102
default: {
99-
'@path': '$.properties.audience_key'
103+
'@if': {
104+
exists: { '@path': '$.properties.audience_key' },
105+
then: { '@path': '$.properties.audience_key' },
106+
else: { '@path': '$.context.personas.computation_key' }
107+
}
100108
}
101109
},
102110
personas_audience_key: {

0 commit comments

Comments
 (0)