Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed setTargetingForGPTAsync() to target specific ad unit codes. #1158

Merged
merged 3 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/prebid.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,21 @@ $$PREBID_GLOBAL$$.getBidResponsesForAdUnitCode = function (adUnitCode) {
* Set query string targeting on all GPT ad units.
* @alias module:$$PREBID_GLOBAL$$.setTargetingForGPTAsync
*/
$$PREBID_GLOBAL$$.setTargetingForGPTAsync = function () {
$$PREBID_GLOBAL$$.setTargetingForGPTAsync = function (adUnits) {
utils.logInfo('Invoking $$PREBID_GLOBAL$$.setTargetingForGPTAsync', arguments);
if (!isGptPubadsDefined()) {
utils.logError('window.googletag is not defined on the page');
return;
}

//get our ad unit codes
var adUnitCodes = targeting.getAllTargeting(adUnits);

//first reset any old targeting
targeting.resetPresetTargeting();
targeting.resetPresetTargeting(adUnitCodes);

//now set new targeting keys
targeting.setTargeting(targeting.getAllTargeting());
targeting.setTargeting(adUnitCodes);

//emit event
events.emit(SET_TARGETING);
Expand Down
5 changes: 3 additions & 2 deletions src/targeting.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ var CONSTANTS = require('./constants.json');
var targeting = exports;
var pbTargetingKeys = [];

targeting.resetPresetTargeting = function() {
targeting.resetPresetTargeting = function(adUnitCode) {
if (isGptPubadsDefined()) {
const adUnitCodes = adUnitCode && adUnitCode.length ? [adUnitCode] : $$PREBID_GLOBAL$$._adUnitCodes;
window.googletag.pubads().getSlots().forEach(slot => {
pbTargetingKeys.forEach(function(key){
// reset only registered adunits
$$PREBID_GLOBAL$$.adUnits.find(function(unit) {
adUnitCodes.find(function(unit) {
if (unit.code === slot.getAdUnitPath() ||
unit.code === slot.getSlotElementId()) {
slot.setTargeting(key, null);
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"/123456/header-bid-tag-1",
"/123456/header-bid-tag-2"
]
}
}
6 changes: 3 additions & 3 deletions test/spec/unit/pbjs_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ describe('Unit: Prebid Module', function () {
it('should set googletag targeting keys after calling setTargetingForGPTAsync function', function () {
var slots = createSlotArrayScenario2();
window.googletag.pubads().setSlots(slots);
$$PREBID_GLOBAL$$.setTargetingForGPTAsync(config.adUnitCodes);
$$PREBID_GLOBAL$$.setTargetingForGPTAsync();

var targeting = [];
slots[1].getTargeting().map(function (value) {
Expand All @@ -399,7 +399,7 @@ describe('Unit: Prebid Module', function () {
var slots = createSlotArray();
window.googletag.pubads().setSlots(slots);

$$PREBID_GLOBAL$$.setTargetingForGPTAsync(config.adUnitCodes);
$$PREBID_GLOBAL$$.setTargetingForGPTAsync(config.adUnitElementIDs);
expect(slots[0].spySetTargeting.args).to.deep.contain.members([['hb_bidder', 'appnexus']]);
});

Expand Down Expand Up @@ -440,7 +440,7 @@ describe('Unit: Prebid Module', function () {
var slots = createSlotArray();
window.googletag.pubads().setSlots(slots);

$$PREBID_GLOBAL$$.setTargetingForGPTAsync(config.adUnitCodes);
$$PREBID_GLOBAL$$.setTargetingForGPTAsync();

var expected = [
[
Expand Down