forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargeting_spec.js
More file actions
1518 lines (1345 loc) · 54 KB
/
targeting_spec.js
File metadata and controls
1518 lines (1345 loc) · 54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {expect} from 'chai';
import {
getGPTSlotsForAdUnits,
filters,
getHighestCpmBidsFromBidPool,
sortByDealAndPriceBucketOrCpm,
targeting as targetingInstance
} from 'src/targeting.js';
import {config} from 'src/config.js';
import {createBidReceived} from 'test/fixtures/fixtures.js';
import { DEFAULT_TARGETING_KEYS, JSON_MAPPING, NATIVE_KEYS, STATUS, TARGETING_KEYS } from 'src/constants.js';
import {auctionManager} from 'src/auctionManager.js';
import * as utils from 'src/utils.js';
import {deepClone} from 'src/utils.js';
import {createBid} from '../../../../src/bidfactory.js';
import { hook, setupBeforeHookFnOnce } from '../../../../src/hook.js';
import {getHighestCpm} from '../../../../src/utils/reducers.js';
function mkBid(bid, status = STATUS.GOOD) {
return Object.assign(createBid(status), bid);
}
const sampleBid = {
'bidderCode': 'rubicon',
'width': '300',
'height': '250',
'statusMessage': 'Bid available',
'adId': '148018fe5e',
'cpm': 0.537234,
'ad': 'markup',
'ad_id': '3163950',
'sizeId': '15',
'requestTimestamp': 1454535718610,
'responseTimestamp': 1454535724863,
'timeToRespond': 123,
'pbLg': '0.50',
'pbMg': '0.50',
'pbHg': '0.53',
'adUnitCode': '/123456/header-bid-tag-0',
'bidder': 'rubicon',
'size': '300x250',
'adserverTargeting': {
'foobar': '300x250',
[TARGETING_KEYS.BIDDER]: 'rubicon',
[TARGETING_KEYS.AD_ID]: '148018fe5e',
[TARGETING_KEYS.PRICE_BUCKET]: '0.53',
[TARGETING_KEYS.DEAL]: '1234'
},
'dealId': '1234',
'netRevenue': true,
'currency': 'USD',
'ttl': 300
};
const bid1 = mkBid(sampleBid);
const bid2 = mkBid({
'bidderCode': 'rubicon',
'width': '300',
'height': '250',
'statusMessage': 'Bid available',
'adId': '5454545',
'cpm': 0.25,
'ad': 'markup',
'ad_id': '3163950',
'sizeId': '15',
'requestTimestamp': 1454535718610,
'responseTimestamp': 1454535724863,
'timeToRespond': 123,
'pbLg': '0.25',
'pbMg': '0.25',
'pbHg': '0.25',
'adUnitCode': '/123456/header-bid-tag-0',
'bidder': 'rubicon',
'size': '300x250',
'adserverTargeting': {
'foobar': '300x250',
[TARGETING_KEYS.BIDDER]: 'rubicon',
[TARGETING_KEYS.AD_ID]: '5454545',
[TARGETING_KEYS.PRICE_BUCKET]: '0.25'
},
'netRevenue': true,
'currency': 'USD',
'ttl': 300
});
const bid3 = mkBid({
'bidderCode': 'rubicon',
'width': '300',
'height': '600',
'statusMessage': 'Bid available',
'adId': '48747745',
'cpm': 0.75,
'ad': 'markup',
'ad_id': '3163950',
'sizeId': '15',
'requestTimestamp': 1454535718610,
'responseTimestamp': 1454535724863,
'timeToRespond': 123,
'pbLg': '0.75',
'pbMg': '0.75',
'pbHg': '0.75',
'adUnitCode': '/123456/header-bid-tag-1',
'bidder': 'rubicon',
'size': '300x600',
'adserverTargeting': {
'foobar': '300x600',
[TARGETING_KEYS.BIDDER]: 'rubicon',
[TARGETING_KEYS.AD_ID]: '48747745',
[TARGETING_KEYS.PRICE_BUCKET]: '0.75'
},
'netRevenue': true,
'currency': 'USD',
'ttl': 300
});
const nativeBid1 = mkBid({
'bidderCode': 'appnexus',
'width': 0,
'height': 0,
'statusMessage': 'Bid available',
'adId': '591e7c9354b633',
'requestId': '24aae81e32d6f6',
'mediaType': 'native',
'source': 'client',
'cpm': 10,
'creativeId': 97494403,
'currency': 'USD',
'netRevenue': true,
'ttl': 300,
'adUnitCode': '/19968336/prebid_native_example_1',
'appnexus': {
'buyerMemberId': 9325
},
'meta': {
'advertiserId': 2529885
},
'native': {
'title': 'This is a Prebid Native Creative',
'body': 'This is a Prebid Native Creative. There are many like it, but this one is mine.',
'sponsoredBy': 'Prebid.org',
'clickUrl': 'http://prebid.org/dev-docs/show-native-ads.html',
'clickTrackers': ['http://www.clickUrl.com/404'],
'impressionTrackers': ['http://imp.trackerUrl.com/it1'],
'javascriptTrackers': '<script>//js script here</script>',
'image': {
'url': 'http://vcdn.adnxs.com/p/creative-image/94/22/cd/0f/9422cd0f-f400-45d3-80f5-2b92629d9257.jpg',
'height': 2250,
'width': 3000
},
'icon': {
'url': 'http://vcdn.adnxs.com/p/creative-image/bd/59/a6/c6/bd59a6c6-0851-411d-a16d-031475a51312.png',
'height': 83,
'width': 127
}
},
'auctionId': '72138a4a-b747-4192-9192-dcc41d675de8',
'responseTimestamp': 1565785219461,
'requestTimestamp': 1565785219405,
'bidder': 'appnexus',
'timeToRespond': 56,
'pbLg': '5.00',
'pbMg': '10.00',
'pbHg': '10.00',
'pbAg': '10.00',
'pbDg': '10.00',
'pbCg': '',
'size': '0x0',
'adserverTargeting': {
[TARGETING_KEYS.BIDDER]: 'appnexus',
[TARGETING_KEYS.AD_ID]: '591e7c9354b633',
[TARGETING_KEYS.PRICE_BUCKET]: '10.00',
[TARGETING_KEYS.SIZE]: '0x0',
[TARGETING_KEYS.SOURCE]: 'client',
[TARGETING_KEYS.FORMAT]: 'native',
[NATIVE_KEYS.title]: 'This is a Prebid Native Creative',
[NATIVE_KEYS.body]: 'This is a Prebid Native Creative. There are many like it, but this one is mine.',
[NATIVE_KEYS.sponsoredBy]: 'Prebid.org',
[NATIVE_KEYS.clickUrl]: 'http://prebid.org/dev-docs/show-native-ads.html',
[NATIVE_KEYS.image]: 'http://vcdn.adnxs.com/p/creative-image/94/22/cd/0f/9422cd0f-f400-45d3-80f5-2b92629d9257.jpg',
[NATIVE_KEYS.icon]: 'http://vcdn.adnxs.com/p/creative-image/bd/59/a6/c6/bd59a6c6-0851-411d-a16d-031475a51312.png'
}
});
const nativeBid2 = mkBid({
'bidderCode': 'dgads',
'width': 0,
'height': 0,
'statusMessage': 'Bid available',
'adId': '6e0aba55ed54e5',
'requestId': '4de26ec83d9661',
'mediaType': 'native',
'source': 'client',
'cpm': 1.90909091,
'creativeId': 'xuidx6c901261b0x2b2',
'currency': 'JPY',
'netRevenue': true,
'ttl': 60,
'referrer': 'http://test.localhost:9999/integrationExamples/gpt/demo_native.html?pbjs_debug=true',
'native': {
'image': {
'url': 'https://ads-tr.bigmining.com/img/300x250.png',
'width': 300,
'height': 250
},
'title': 'Test Title',
'body': 'Test Description',
'sponsoredBy': 'test.com',
'clickUrl': 'http://prebid.org/',
'clickTrackers': ['https://www.clickUrl.com/404'],
'impressionTrackers': [
'http://imp.trackerUrl.com/it2'
]
},
'auctionId': '72138a4a-b747-4192-9192-dcc41d675de8',
'responseTimestamp': 1565785219607,
'requestTimestamp': 1565785219409,
'bidder': 'dgads',
'adUnitCode': '/19968336/prebid_native_example_1',
'timeToRespond': 198,
'pbLg': '1.50',
'pbMg': '1.90',
'pbHg': '1.90',
'pbAg': '1.90',
'pbDg': '1.90',
'pbCg': '',
'size': '0x0',
'adserverTargeting': {
[TARGETING_KEYS.BIDDER]: 'dgads',
[TARGETING_KEYS.AD_ID]: '6e0aba55ed54e5',
[TARGETING_KEYS.PRICE_BUCKET]: '1.90',
[TARGETING_KEYS.SIZE]: '0x0',
[TARGETING_KEYS.SOURCE]: 'client',
[TARGETING_KEYS.FORMAT]: 'native',
[NATIVE_KEYS.image]: 'https://ads-tr.bigmining.com/img/300x250.png',
[NATIVE_KEYS.title]: 'Test Title',
[NATIVE_KEYS.body]: 'Test Description',
[NATIVE_KEYS.sponsoredBy]: 'test.com',
[NATIVE_KEYS.clickUrl]: 'http://prebid.org/'
}
});
describe('targeting tests', function () {
let sandbox;
let enableSendAllBids = false;
let useBidCache;
let bidCacheFilterFunction;
let undef;
before(() => {
hook.ready();
});
beforeEach(function() {
sandbox = sinon.sandbox.create();
useBidCache = true;
let origGetConfig = config.getConfig;
sandbox.stub(config, 'getConfig').callsFake(function (key) {
if (key === 'enableSendAllBids') {
return enableSendAllBids;
}
if (key === 'useBidCache') {
return useBidCache;
}
if (key === 'bidCacheFilterFunction') {
return bidCacheFilterFunction;
}
return origGetConfig.apply(config, arguments);
});
});
afterEach(function () {
sandbox.restore();
bidCacheFilterFunction = undef;
});
describe('isBidNotExpired', () => {
let clock;
beforeEach(() => {
clock = sandbox.useFakeTimers(0);
});
Object.entries({
'bid.ttlBuffer': (bid, ttlBuffer) => {
bid.ttlBuffer = ttlBuffer
},
'setConfig({ttlBuffer})': (_, ttlBuffer) => {
config.setConfig({ttlBuffer})
},
}).forEach(([t, setup]) => {
describe(`respects ${t}`, () => {
[0, 2].forEach(ttlBuffer => {
it(`when ttlBuffer is ${ttlBuffer}`, () => {
const bid = {
responseTimestamp: 0,
ttl: 10,
}
setup(bid, ttlBuffer);
expect(filters.isBidNotExpired(bid)).to.be.true;
clock.tick((bid.ttl - ttlBuffer) * 1000 - 100);
expect(filters.isBidNotExpired(bid)).to.be.true;
clock.tick(101);
expect(filters.isBidNotExpired(bid)).to.be.false;
});
});
});
});
});
describe('getAllTargeting', function () {
let amBidsReceivedStub;
let amGetAdUnitsStub;
let bidExpiryStub;
let logWarnStub;
let logErrorStub;
let bidsReceived;
beforeEach(function () {
bidsReceived = [bid1, bid2, bid3].map(deepClone);
amBidsReceivedStub = sandbox.stub(auctionManager, 'getBidsReceived').callsFake(function() {
return bidsReceived;
});
amGetAdUnitsStub = sandbox.stub(auctionManager, 'getAdUnitCodes').callsFake(function() {
return ['/123456/header-bid-tag-0'];
});
bidExpiryStub = sandbox.stub(filters, 'isBidNotExpired').returns(true);
logWarnStub = sinon.stub(utils, 'logWarn');
logErrorStub = sinon.stub(utils, 'logError');
});
afterEach(function() {
config.resetConfig();
logWarnStub.restore();
logErrorStub.restore();
amBidsReceivedStub.restore();
amGetAdUnitsStub.restore();
bidExpiryStub.restore();
});
describe('when handling different adunit targeting value types', function () {
const adUnitCode = '/123456/header-bid-tag-0';
const adServerTargeting = {};
let getAdUnitsStub;
before(function() {
getAdUnitsStub = sandbox.stub(auctionManager, 'getAdUnits').callsFake(function() {
return [
{
'code': adUnitCode,
[JSON_MAPPING.ADSERVER_TARGETING]: adServerTargeting
}
];
});
});
after(function() {
getAdUnitsStub.restore();
});
afterEach(function() {
delete adServerTargeting.test_type;
});
const pairs = [
['string', '2.3', '2.3'],
['number', 2.3, '2.3'],
['boolean', true, 'true'],
['string-separated', '2.3, 4.5', '2.3,4.5'],
['array-of-string', ['2.3', '4.5'], '2.3,4.5'],
['array-of-number', [2.3, 4.5], '2.3,4.5'],
['array-of-boolean', [true, false], 'true,false']
];
pairs.forEach(([type, value, result]) => {
it(`accepts ${type}`, function() {
adServerTargeting.test_type = value;
const targeting = targetingInstance.getAllTargeting([adUnitCode]);
expect(targeting[adUnitCode].test_type).is.equal(result);
});
});
});
describe('when hb_deal is present in bid.adserverTargeting', function () {
let bid4;
beforeEach(function() {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting['hb_bidder'] = bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 0;
enableSendAllBids = true;
bidsReceived.push(bid4);
});
after(function() {
config.setConfig({
targetingControls: {
alwaysIncludeDeals: false
}
});
enableSendAllBids = false;
})
it('returns targeting with both hb_deal and hb_deal_{bidder_code}', function () {
config.setConfig({
targetingControls: {
alwaysIncludeDeals: true
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// We should add both keys rather than one or the other
expect(targeting['/123456/header-bid-tag-0']).to.contain.keys('hb_deal', `hb_deal_${bid1.bidderCode}`, `hb_deal_${bid4.bidderCode}`);
// We should assign both keys the same value
expect(targeting['/123456/header-bid-tag-0']['hb_deal']).to.deep.equal(targeting['/123456/header-bid-tag-0'][`hb_deal_${bid1.bidderCode}`]);
});
});
it('will enforce a limit on the number of auction keys when auctionKeyMaxChars setting is active', function () {
config.setConfig({
targetingControls: {
auctionKeyMaxChars: 150
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0', '/123456/header-bid-tag-1']);
expect(targeting['/123456/header-bid-tag-1']).to.deep.equal({});
expect(targeting['/123456/header-bid-tag-0']).to.contain.keys('hb_pb', 'hb_adid', 'hb_bidder', 'hb_deal');
expect(targeting['/123456/header-bid-tag-0']['hb_adid']).to.equal(bid1.adId);
expect(logWarnStub.calledOnce).to.be.true;
});
it('will return an error when auctionKeyMaxChars setting is set too low for any auction keys to be allowed', function () {
config.setConfig({
targetingControls: {
auctionKeyMaxChars: 50
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0', '/123456/header-bid-tag-1']);
expect(targeting['/123456/header-bid-tag-1']).to.deep.equal({});
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({});
expect(logWarnStub.calledTwice).to.be.true;
expect(logErrorStub.calledOnce).to.be.true;
});
describe('when bidLimit is present in setConfig', function () {
let bid4;
beforeEach(function() {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting['hb_bidder'] = bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 2.25;
bid4.adId = '8383838';
enableSendAllBids = true;
bidsReceived.push(bid4);
});
it('when sendBidsControl.bidLimit is set greater than 0 in getHighestCpmBidsFromBidPool', function () {
config.setConfig({
sendBidsControl: {
bidLimit: 2,
dealPrioritization: true
}
});
const bids = getHighestCpmBidsFromBidPool(bidsReceived, getHighestCpm, 2);
expect(bids.length).to.equal(3);
expect(bids[0].adId).to.equal('8383838');
expect(bids[1].adId).to.equal('148018fe5e');
expect(bids[2].adId).to.equal('48747745');
});
it('when sendBidsControl.bidLimit is set greater than 0 and deal priortization is false in getHighestCpmBidsFromBidPool', function () {
config.setConfig({
sendBidsControl: {
bidLimit: 2,
dealPrioritization: false
}
});
const bids = getHighestCpmBidsFromBidPool(bidsReceived, getHighestCpm, 2);
expect(bids.length).to.equal(3);
expect(bids[0].adId).to.equal('8383838');
expect(bids[1].adId).to.equal('148018fe5e');
expect(bids[2].adId).to.equal('48747745');
});
it('selects the top n number of bids when enableSendAllBids is true and and bitLimit is set', function () {
config.setConfig({
sendBidsControl: {
bidLimit: 1
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
let limitedBids = Object.keys(targeting['/123456/header-bid-tag-0']).filter(key => key.indexOf(TARGETING_KEYS.PRICE_BUCKET + '_') != -1)
expect(limitedBids.length).to.equal(1);
});
it('sends all bids when enableSendAllBids is true and and bitLimit is above total number of bids received', function () {
config.setConfig({
sendBidsControl: {
bidLimit: 50
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
let limitedBids = Object.keys(targeting['/123456/header-bid-tag-0']).filter(key => key.indexOf(TARGETING_KEYS.PRICE_BUCKET + '_') != -1)
expect(limitedBids.length).to.equal(2);
});
it('Sends all bids when enableSendAllBids is true and and bidLimit is set to 0', function () {
config.setConfig({
sendBidsControl: {
bidLimit: 0
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
let limitedBids = Object.keys(targeting['/123456/header-bid-tag-0']).filter(key => key.indexOf(TARGETING_KEYS.PRICE_BUCKET + '_') != -1)
expect(limitedBids.length).to.equal(2);
});
});
describe('targetingControls.allowZeroCpmBids', function () {
let bid4;
let bidderSettingsStorage;
before(function() {
bidderSettingsStorage = $$PREBID_GLOBAL$$.bidderSettings;
});
beforeEach(function () {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting = {
hb_pb: '0.0',
hb_adid: '567891011',
hb_bidder: 'appnexus',
};
bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 0;
bidsReceived = [bid4];
});
after(function() {
$$PREBID_GLOBAL$$.bidderSettings = bidderSettingsStorage;
enableSendAllBids = false;
})
it('targeting should not include a 0 cpm by default', function() {
bid4.adserverTargeting = {};
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({});
});
it('targeting should allow a 0 cpm with targetingControls.allowZeroCpmBids set to true', function () {
$$PREBID_GLOBAL$$.bidderSettings = {
standard: {
allowZeroCpmBids: true
}
};
enableSendAllBids = true;
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_pb', 'hb_bidder', 'hb_adid', 'hb_bidder_appnexus', 'hb_adid_appnexus', 'hb_pb_appnexus');
expect(targeting['/123456/header-bid-tag-0']['hb_pb']).to.equal('0.0')
});
});
describe('targetingControls.allowTargetingKeys', function () {
let bid4;
beforeEach(function() {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting = {
hb_deal: '4321',
hb_pb: '0.1',
hb_adid: '567891011',
hb_bidder: 'appnexus',
};
bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 0.1; // losing bid so not included if enableSendAllBids === false
bid4.dealId = '4321';
enableSendAllBids = true;
config.setConfig({
targetingControls: {
allowTargetingKeys: ['BIDDER', 'AD_ID', 'PRICE_BUCKET']
}
});
bidsReceived.push(bid4);
});
it('targeting should include custom keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('foobar');
});
it('targeting should include keys prefixed by allowed default targeting keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_bidder_rubicon', 'hb_adid_rubicon', 'hb_pb_rubicon');
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_bidder_appnexus', 'hb_adid_appnexus', 'hb_pb_appnexus');
});
it('targeting should not include keys prefixed by disallowed default targeting keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.not.have.all.keys(['hb_deal_appnexus', 'hb_deal_rubicon']);
});
});
describe('targetingControls.addTargetingKeys', function () {
let winningBid = null;
beforeEach(function () {
bidsReceived = [bid1, bid2, nativeBid1, nativeBid2].map(deepClone);
bidsReceived.forEach((bid) => {
bid.adserverTargeting[TARGETING_KEYS.SOURCE] = 'test-source';
bid.adUnitCode = 'adunit';
if (winningBid == null || bid.cpm > winningBid.cpm) {
winningBid = bid;
}
});
enableSendAllBids = true;
});
const expandKey = function (key) {
const keys = new Set();
if (winningBid.adserverTargeting[key] != null) {
keys.add(key);
}
bidsReceived
.filter((bid) => bid.adserverTargeting[key] != null)
.map((bid) => bid.bidderCode)
.forEach((code) => keys.add(`${key}_${code}`.substr(0, 20)));
return new Array(...keys);
}
const targetingResult = function () {
return targetingInstance.getAllTargeting(['adunit'])['adunit'];
}
it('should include added keys', function () {
config.setConfig({
targetingControls: {
addTargetingKeys: ['SOURCE']
}
});
expect(targetingResult()).to.include.all.keys(...expandKey(TARGETING_KEYS.SOURCE));
});
it('should keep default and native keys', function() {
config.setConfig({
targetingControls: {
addTargetingKeys: ['SOURCE']
}
});
const defaultKeys = new Set(Object.values(DEFAULT_TARGETING_KEYS));
if (FEATURES.NATIVE) {
Object.values(NATIVE_KEYS).forEach((k) => defaultKeys.add(k));
}
const expectedKeys = new Set();
bidsReceived
.map((bid) => Object.keys(bid.adserverTargeting))
.reduce((left, right) => left.concat(right), [])
.filter((key) => defaultKeys.has(key))
.map(expandKey)
.reduce((left, right) => left.concat(right), [])
.forEach((k) => expectedKeys.add(k));
expect(targetingResult()).to.include.all.keys(...expectedKeys);
});
it('should not be allowed together with allowTargetingKeys', function () {
config.setConfig({
targetingControls: {
allowTargetingKeys: [TARGETING_KEYS.BIDDER],
addTargetingKeys: [TARGETING_KEYS.SOURCE]
}
});
expect(targetingResult).to.throw();
});
});
describe('targetingControls.allowSendAllBidsTargetingKeys', function () {
let bid4;
beforeEach(function() {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting = {
hb_deal: '4321',
hb_pb: '0.1',
hb_adid: '567891011',
hb_bidder: 'appnexus',
};
bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 0.1; // losing bid so not included if enableSendAllBids === false
bid4.dealId = '4321';
enableSendAllBids = true;
config.setConfig({
targetingControls: {
allowTargetingKeys: ['BIDDER', 'AD_ID', 'PRICE_BUCKET'],
allowSendAllBidsTargetingKeys: ['PRICE_BUCKET', 'AD_ID']
}
});
bidsReceived.push(bid4);
});
it('targeting should include custom keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('foobar');
});
it('targeting should only include keys prefixed by allowed default send all bids targeting keys and standard keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_bidder', 'hb_adid', 'hb_pb');
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_adid_rubicon', 'hb_pb_rubicon');
expect(targeting['/123456/header-bid-tag-0']).to.include.all.keys('hb_bidder', 'hb_adid', 'hb_pb', 'hb_adid_appnexus', 'hb_pb_appnexus');
});
it('targeting should not include keys prefixed by disallowed default targeting keys and disallowed send all bid targeting keys', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.not.have.all.keys(['hb_deal', 'hb_bidder_rubicon', 'hb_bidder_appnexus', 'hb_deal_appnexus', 'hb_deal_rubicon']);
});
});
describe('targetingControls.alwaysIncludeDeals', function () {
let bid4;
beforeEach(function() {
bid4 = utils.deepClone(bid1);
bid4.adserverTargeting = {
hb_deal: '4321',
hb_pb: '0.1',
hb_adid: '567891011',
hb_bidder: 'appnexus',
};
bid4.bidder = bid4.bidderCode = 'appnexus';
bid4.cpm = 0.1; // losing bid so not included if enableSendAllBids === false
bid4.dealId = '4321';
enableSendAllBids = false;
bidsReceived.push(bid4);
});
it('does not include losing deals when alwaysIncludeDeals not set', function () {
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// Rubicon wins bid and has deal, but alwaysIncludeDeals is false, so only top bid plus deal_id
// appnexus does not get sent since alwaysIncludeDeals is not defined
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({
'hb_deal_rubicon': '1234',
'hb_deal': '1234',
'hb_pb': '0.53',
'hb_adid': '148018fe5e',
'hb_bidder': 'rubicon',
'foobar': '300x250'
});
});
it('does not include losing deals when alwaysIncludeDeals set to false', function () {
config.setConfig({
targetingControls: {
alwaysIncludeDeals: false
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// Rubicon wins bid and has deal, but alwaysIncludeDeals is false, so only top bid plus deal_id
// appnexus does not get sent since alwaysIncludeDeals is false
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({
'hb_deal_rubicon': '1234', // This is just how it works before this PR, always added no matter what for winner if they have deal
'hb_deal': '1234',
'hb_pb': '0.53',
'hb_adid': '148018fe5e',
'hb_bidder': 'rubicon',
'foobar': '300x250'
});
});
it('includes losing deals when alwaysIncludeDeals set to true and also winning deals bidder KVPs', function () {
config.setConfig({
targetingControls: {
alwaysIncludeDeals: true
}
});
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// Rubicon wins bid and has a deal, so all KVPs for them are passed (top plus bidder specific)
// Appnexus had deal so passed through
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({
'hb_deal_rubicon': '1234',
'hb_deal': '1234',
'hb_pb': '0.53',
'hb_adid': '148018fe5e',
'hb_bidder': 'rubicon',
'foobar': '300x250',
'hb_pb_rubicon': '0.53',
'hb_adid_rubicon': '148018fe5e',
'hb_bidder_rubicon': 'rubicon',
'hb_deal_appnexus': '4321',
'hb_pb_appnexus': '0.1',
'hb_adid_appnexus': '567891011',
'hb_bidder_appnexus': 'appnexus'
});
});
it('includes winning bid even when it is not a deal, plus other deal KVPs', function () {
config.setConfig({
targetingControls: {
alwaysIncludeDeals: true
}
});
let bid5 = utils.deepClone(bid4);
bid5.adserverTargeting = {
hb_pb: '3.0',
hb_adid: '111111',
hb_bidder: 'pubmatic',
foobar: '300x250'
};
bid5.bidder = bid5.bidderCode = 'pubmatic';
bid5.cpm = 3.0; // winning bid!
delete bid5.dealId; // no deal with winner
bidsReceived.push(bid5);
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// Pubmatic wins but no deal. So only top bid KVPs for them is sent
// Rubicon has a dealId so passed through
// Appnexus has a dealId so passed through
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({
'hb_bidder': 'pubmatic',
'hb_adid': '111111',
'hb_pb': '3.0',
'foobar': '300x250',
'hb_deal_rubicon': '1234',
'hb_pb_rubicon': '0.53',
'hb_adid_rubicon': '148018fe5e',
'hb_bidder_rubicon': 'rubicon',
'hb_deal_appnexus': '4321',
'hb_pb_appnexus': '0.1',
'hb_adid_appnexus': '567891011',
'hb_bidder_appnexus': 'appnexus'
});
});
});
describe('targetingControls.alwaysIncludeDeals with enableSendAllBids', function () {
beforeEach(function() {
enableSendAllBids = true;
});
it('includes bids w/o deal when enableSendAllBids and alwaysIncludeDeals set to true', function () {
config.setConfig({
enableSendAllBids: true,
targetingControls: {
alwaysIncludeDeals: true
}
});
let bid5 = utils.deepClone(bid1);
bid5.adserverTargeting = {
hb_pb: '3.0',
hb_adid: '111111',
hb_bidder: 'pubmatic',
foobar: '300x250'
};
bid5.bidder = bid5.bidderCode = 'pubmatic';
bid5.cpm = 3.0; // winning bid!
delete bid5.dealId; // no deal with winner
bidsReceived.push(bid5);
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// Pubmatic wins but no deal. But enableSendAllBids is true.
// So Pubmatic is passed through
expect(targeting['/123456/header-bid-tag-0']).to.deep.equal({
'hb_bidder': 'pubmatic',
'hb_adid': '111111',
'hb_pb': '3.0',
'foobar': '300x250',
'hb_pb_pubmatic': '3.0',
'hb_adid_pubmatic': '111111',
'hb_bidder_pubmatic': 'pubmatic',
'hb_deal_rubicon': '1234',
'hb_pb_rubicon': '0.53',
'hb_adid_rubicon': '148018fe5e',
'hb_bidder_rubicon': 'rubicon'
});
});
});
it('selects the top bid when enableSendAllBids true', function () {
enableSendAllBids = true;
let targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
// we should only get the targeting data for the one requested adunit
expect(Object.keys(targeting).length).to.equal(1);
let sendAllBidCpm = Object.keys(targeting['/123456/header-bid-tag-0']).filter(key => key.indexOf(TARGETING_KEYS.PRICE_BUCKET + '_') != -1)
// we shouldn't get more than 1 key for hb_pb_${bidder}
expect(sendAllBidCpm.length).to.equal(1);
// expect the winning CPM to be equal to the sendAllBidCPM
expect(targeting['/123456/header-bid-tag-0'][TARGETING_KEYS.PRICE_BUCKET + '_rubicon']).to.deep.equal(targeting['/123456/header-bid-tag-0'][TARGETING_KEYS.PRICE_BUCKET]);
});
if (FEATURES.NATIVE) {
it('ensures keys are properly generated when enableSendAllBids is true and multiple bidders use native', function () {
const nativeAdUnitCode = '/19968336/prebid_native_example_1';
enableSendAllBids = true;
// update mocks for this test to return native bids
amBidsReceivedStub.callsFake(function () {
return [nativeBid1, nativeBid2];
});
amGetAdUnitsStub.callsFake(function () {
return [nativeAdUnitCode];
});
let targeting = targetingInstance.getAllTargeting([nativeAdUnitCode]);
expect(targeting[nativeAdUnitCode].hb_native_image).to.equal(nativeBid1.native.image.url);
expect(targeting[nativeAdUnitCode].hb_native_linkurl).to.equal(nativeBid1.native.clickUrl);
expect(targeting[nativeAdUnitCode].hb_native_title).to.equal(nativeBid1.native.title);
expect(targeting[nativeAdUnitCode].hb_native_image_dgad).to.exist.and.to.equal(nativeBid2.native.image.url);
expect(targeting[nativeAdUnitCode].hb_pb_dgads).to.exist.and.to.equal(nativeBid2.pbMg);
expect(targeting[nativeAdUnitCode].hb_native_body_appne).to.exist.and.to.equal(nativeBid1.native.body);
});
}
it('does not include adpod type bids in the getBidsReceived results', function () {
let adpodBid = utils.deepClone(bid1);
adpodBid.video = { context: 'adpod', durationSeconds: 15, durationBucket: 15 };
adpodBid.cpm = 5;
bidsReceived.push(adpodBid);
const targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']).to.contain.keys('hb_deal', 'hb_adid', 'hb_bidder');
expect(targeting['/123456/header-bid-tag-0']['hb_adid']).to.equal(bid1.adId);
});
}); // end getAllTargeting tests
describe('getAllTargeting will work correctly when a hook raises has modified flag in getHighestCpmBidsFromBidPool', function () {
let bidsReceived;
let amGetAdUnitsStub;
let amBidsReceivedStub;
let bidExpiryStub;
beforeEach(function () {
bidsReceived = [bid2, bid1].map(deepClone);
amBidsReceivedStub = sandbox.stub(auctionManager, 'getBidsReceived').callsFake(function() {
return bidsReceived;
});
amGetAdUnitsStub = sandbox.stub(auctionManager, 'getAdUnitCodes').callsFake(function() {
return ['/123456/header-bid-tag-0'];
});
bidExpiryStub = sandbox.stub(filters, 'isBidNotExpired').returns(true);
setupBeforeHookFnOnce(getHighestCpmBidsFromBidPool, function (fn, bidsReceived, highestCpmCallback, adUnitBidLimit = 0, hasModified = false) {
fn.call(this, bidsReceived, highestCpmCallback, adUnitBidLimit, true);
});
});
afterEach(function () {
getHighestCpmBidsFromBidPool.getHooks().remove();
})
it('will apply correct targeting', function () {
let targeting = targetingInstance.getAllTargeting(['/123456/header-bid-tag-0']);
expect(targeting['/123456/header-bid-tag-0']['hb_pb']).to.equal('0.53');
expect(targeting['/123456/header-bid-tag-0']['hb_adid']).to.equal('148018fe5e');
})
});
describe('getAllTargeting without bids return empty object', function () {
let amBidsReceivedStub;
let amGetAdUnitsStub;
let bidExpiryStub;
beforeEach(function () {
enableSendAllBids = false;
amBidsReceivedStub = sandbox.stub(auctionManager, 'getBidsReceived').callsFake(function() {