@@ -34,10 +34,12 @@ This page has documentation for the public API methods of Prebid.js.
34
34
* [ .requestBids(requestObj)] ( #module_pbjs.requestBids )
35
35
* [ .addAdUnits(Array)] ( #module_pbjs.addAdUnits )
36
36
* [ .bidderSettings] ( #module_pbjs.bidderSettings )
37
- * [ .addCallback(event, func)] ( #module_pbjs.addCallback ) ⇒ ` String `
38
- * [ .removeCallback(cbId)] ( #module_pbjs.removeCallback ) ⇒ ` String `
37
+ * [ .addCallback(event, func)] ( #module_pbjs.addCallback )
38
+ * [ .removeCallback(cbId)] ( #module_pbjs.removeCallback )
39
39
* [ .buildMasterVideoTagFromAdserverTag(adserverTag, options)] ( #module_pbjs.buildMasterVideoTagFromAdserverTag ) ⇒ ` String `
40
40
* [ .setBidderSequence(order)] ( #module_pbjs.setBidderSequence )
41
+ * [ .onEvent(event, handler, id)] ( #module_pbjs.onEvent )
42
+ * [ .offEvent(event, handler, id)] ( #module_pbjs.onEvent )
41
43
42
44
<a name =" module_pbjs.getAdserverTargeting " ></a >
43
45
@@ -762,6 +764,10 @@ If a custom adServerTargeting function can return an empty value, this boolean f
762
764
<a name =" module_pbjs.addCallback " ></a >
763
765
764
766
### pbjs.addCallback(event, func) ⇒ ` String `
767
+
768
+ {: .alert.alert-danger :}
769
+ This method is deprecated. Please use [ ` onEvent ` ] ( #module_pbjs.onEvent ) or [ ` offEvent ` ] ( #module_pbjs.onEvent ) instead.
770
+
765
771
Add a callback event
766
772
767
773
** Kind** : static method of [ pbjs] ( #module_pbjs )
@@ -779,6 +785,10 @@ Add a callback event
779
785
<a name =" module_pbjs.removeCallback " ></a >
780
786
781
787
### pbjs.removeCallback(cbId) ⇒ ` String `
788
+
789
+ {: .alert.alert-danger :}
790
+ This method is deprecated. Please use [ ` onEvent ` ] ( #module_pbjs.onEvent ) or [ ` offEvent ` ] ( #module_pbjs.onEvent ) instead.
791
+
782
792
Remove a callback event
783
793
784
794
** Kind** : static method of [ pbjs] ( #module_pbjs )
@@ -843,4 +853,89 @@ Example use:
843
853
pbjs .setBidderSequence (' random' );
844
854
```
845
855
856
+ <a name =" module_pbjs.onEvent " ></a >
857
+
858
+ <p >
859
+ </p >
860
+
861
+ ### pbjs.onEvent(event, handler, id)
862
+
863
+ ** pbjs.offEvent(event, handler, id)**
864
+
865
+ The methods ` onEvent ` and ` offEvent ` are provided for you to register
866
+ a callback to handle a Prebid.js event.
867
+
868
+ They replace the following deprecated methods:
869
+
870
+ - [ .addCallback(event, func)] ( #module_pbjs.addCallback )
871
+ - [ .removeCallback(cbId)] ( #module_pbjs.removeCallback )
872
+
873
+ The optional ` id ` parameter provides more finely-grained event
874
+ callback registration. This makes it possible to register callback
875
+ events for a specific item in the event context.
876
+
877
+ For example, ` bidWon ` events will accept an ` id ` for ad unit code.
878
+ ` bidWon ` callbacks registered with an ad unit code id will be called
879
+ when a bid for that ad unit code wins the auction. Without an ` id `
880
+ this method registers the callback for every ` bidWon ` event.
881
+
882
+ {: .alert.alert-info :}
883
+ Currently, ` bidWon ` is the only event that accepts the ` id ` parameter.
884
+
885
+ The available events are:
886
+
887
+ {: .table .table-bordered .table-striped }
888
+ | Event | Description |
889
+ | ---------------+----------------------------------------|
890
+ | auctionInit | The auction has started |
891
+ | auctionEnd | The auction has ended |
892
+ | bidAdjustment | A bid was adjusted |
893
+ | bidTimeout | A bid timed out |
894
+ | bidRequested | A bid was requested |
895
+ | bidResponse | A bid response has arrived |
896
+ | bidWon | A bid has won |
897
+ | setTargeting | Targeting has been set |
898
+ | requestBids | Bids have been requested from adapters |
899
+
900
+ The example below shows how to use these methods:
901
+
902
+ {% highlight js %}
903
+
904
+ /* Define your event handler callbacks */
905
+ var allSlotsBidWon = function allSlotsBidWon() {
906
+ console.log('allSlotsBidWon called');
907
+ };
908
+
909
+ /* In this event handler callback we use the `pbjs.offEvent`
910
+ method to remove the handler once it has been called */
911
+ var rightSlotBidWon = function rightSlotBidWon() {
912
+ console.log('rightSlotBidWon: ', arguments);
913
+ pbjs.offEvent('bidWon', rightSlotBidWon, rightSlotCode);
914
+ };
915
+
916
+ googletag.cmd.push(function () {
917
+
918
+ /* Ad slots need to be defined before trying to register
919
+ callbacks on their events */
920
+
921
+ var rightSlot =
922
+ googletag.defineSlot(rightSlotCode, rightSlotSizes, rightSlotElementId).addService(googletag.pubads());
923
+
924
+ var topSlot =
925
+ googletag.defineSlot(topSlotCode, topSlotSizes, topSlotElementId).setTargeting().addService(googletag.pubads());
926
+
927
+ pbjs.que.push(function () {
928
+
929
+ /* Register a callback for every `bidWon` event */
930
+ pbjs.onEvent('bidWon', allSlotsBidWon);
931
+
932
+ /* Register a callback for just the rightSlot `bidWon`
933
+ event */
934
+ pbjs.onEvent('bidWon', rightSlotBidWon, rightSlotCode);
935
+
936
+ pbjs.setTargetingForGPTAsync();
937
+ ...
938
+
939
+ {% endhighlight %}
940
+
846
941
</div >
0 commit comments