Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6b0bb5

Browse files
authored
Documentation on auction events (prebid#245)
* WIP docs on events * Final pass on first draft events docs
1 parent 3752fb9 commit d6b0bb5

File tree

1 file changed

+97
-2
lines changed

1 file changed

+97
-2
lines changed

dev-docs/publisher-api-reference.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ This page has documentation for the public API methods of Prebid.js.
3434
* [.requestBids(requestObj)](#module_pbjs.requestBids)
3535
* [.addAdUnits(Array)](#module_pbjs.addAdUnits)
3636
* [.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)
3939
* [.buildMasterVideoTagFromAdserverTag(adserverTag, options)](#module_pbjs.buildMasterVideoTagFromAdserverTag)`String`
4040
* [.setBidderSequence(order)](#module_pbjs.setBidderSequence)
41+
* [.onEvent(event, handler, id)](#module_pbjs.onEvent)
42+
* [.offEvent(event, handler, id)](#module_pbjs.onEvent)
4143

4244
<a name="module_pbjs.getAdserverTargeting"></a>
4345

@@ -762,6 +764,10 @@ If a custom adServerTargeting function can return an empty value, this boolean f
762764
<a name="module_pbjs.addCallback"></a>
763765

764766
### 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+
765771
Add a callback event
766772

767773
**Kind**: static method of [pbjs](#module_pbjs)
@@ -779,6 +785,10 @@ Add a callback event
779785
<a name="module_pbjs.removeCallback"></a>
780786

781787
### 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+
782792
Remove a callback event
783793

784794
**Kind**: static method of [pbjs](#module_pbjs)
@@ -843,4 +853,89 @@ Example use:
843853
pbjs.setBidderSequence('random');
844854
```
845855

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+
846941
</div>

0 commit comments

Comments
 (0)