@@ -401,6 +401,232 @@ describe('LinkedinAudiences.updateAudience', () => {
401
401
'"{\\"elements\\":[{\\"action\\":\\"ADD\\",\\"userIds\\":[{\\"idType\\":\\"SHA256_EMAIL\\",\\"idValue\\":\\"584c4423c421df49955759498a71495aba49b8780eb9387dff333b6f0982c777\\"},{\\"idType\\":\\"GOOGLE_AID\\",\\"idValue\\":\\"123\\"}],\\"firstName\\":\\"John\\",\\"lastName\\":\\"Doe\\",\\"title\\":\\"CEO\\",\\"company\\":\\"Acme\\",\\"country\\":\\"US\\"}]}"'
402
402
)
403
403
} )
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
+ } )
404
630
} )
405
631
406
632
describe ( 'Error cases' , ( ) => {
0 commit comments