Bug - setPriceGranularity is not a function
Type of issue
Bug
Description
I'm trying to change the default price granularity, but when I call the method, it throws an error saying it isn't a function. I do load the prebid.js before and placed the method call at the same location like in the example on the website.
Implementation
<!-- preBid load script -->
<script type="text/javascript" src="/prebid.js?v001" async></script>
<!-- Prebid Config Section START -->
<!-- Make sure this is inserted before your GPT tag -->
<script>
var mobile = false;
var PREBID_TIMEOUT = 1400;
if (document.documentElement.clientWidth <= 800) {
mobile = true;
}
if (mobile) {
PREBID_TIMEOUT = 2400;
var adUnits = [
{
code: 'div-gpt-ad-ClashOfClans_mobile_header',
sizes: [[300, 250],[336, 280],[320, 480],[320, 50],[300, 50],[468, 60],[250, 250],[216, 36],[180, 150],[168, 28],[120, 20]],
bids: [{
bidder: 'sovrn',
params: {
tagid: '480204' // 300x250
}
},{
bidder: 'sovrn',
params: {
tagid: '480205' // 250x250
}
},{
bidder: 'sovrn',
params: {
tagid: '480206' // 468x60
}
},{
bidder: 'sovrn',
params: {
tagid: '480207' // 336x280
}
},{
bidder: 'sovrn',
params: {
tagid: '480208' // 320x50
}
},{
bidder: 'sovrn',
params: {
tagid: '480209' // 300x50
}
},{
bidder: 'sovrn',
params: {
tagid: '480210' // 320x480
}
},{
bidder: 'pulsepoint',
params: {
cf: '300X250',
cp: 561199,
ct: 584122 //300x250
}
},{
bidder: 'pulsepoint',
params: {
cf: '300X50',
cp: 561199,
ct: 584123 //300x50
}
},{
bidder: 'pulsepoint',
params: {
cf: '320X480',
cp: 561199,
ct: 584125 //320x480
}
},{
bidder: 'pulsepoint',
params: {
cf: '320X50',
cp: 561199,
ct: 584126 //320x50
}
},{
bidder: 'pulsepoint',
params: {
cf: '216X36',
cp: 561199,
ct: 584127 //216x36
}
},{
bidder: 'pulsepoint',
params: {
cf: '180X150',
cp: 561199,
ct: 584128 //180x150
}
},{
bidder: 'pulsepoint',
params: {
cf: '168X28',
cp: 561199,
ct: 584129 //168x28
}
},{
bidder: 'pulsepoint',
params: {
cf: '120X20',
cp: 561199,
ct: 584130 //120x20
}
},{
bidder: 'roxot',
params: {
publisherId: '62770cad-91a5-4cd7-be30-22ccf9e1b63c'
}
}
]
}
];
} else {
var adUnits = [{
code: 'div-gpt-ad-ClashOfClans_header',
sizes: [[970, 250],[800, 250],[728, 90]],
bids: [{
bidder: 'sovrn',
params: {
tagid: '479432' // 970x250
}
},{
bidder: 'sovrn',
params: {
tagid: '479433' // 728x90
}
},{
bidder: 'pulsepoint',
params: {
cf: '970X250',
cp: 561199,
ct: 583550 //970x250
}
},{
bidder: 'pulsepoint',
params: {
cf: '800X250',
cp: 561199,
ct: 583551 // 800x250
}
},{
bidder: 'pulsepoint',
params: {
cf: '728X90',
cp: 561199,
ct: 583552 // 728x90
}
},{
bidder: 'roxot',
params: {
publisherId: '62770cad-91a5-4cd7-be30-22ccf9e1b63c'
}
}]
},
{
code: 'div-gpt-ad-ClashOfClans_sidebarLeft',
sizes: [[160, 600],[120, 600]],
bids: [{
bidder: 'sovrn',
params: {
tagid: '469528' // 160x600
}
},{
bidder: 'sovrn',
params: {
tagid: '478635' // 120x600
}
},{
bidder: 'pulsepoint',
params: {
cf: '160X600',
cp: 561199,
ct: 582815 // 160x600
}
},{
bidder: 'pulsepoint',
params: {
cf: '120X600',
cp: 561199,
ct: 582861 // 120x600
}
},{
bidder: 'roxot',
params: {
publisherId: '62770cad-91a5-4cd7-be30-22ccf9e1b63c'
}
}]
}
];
}
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
pbjs.setPriceGranularity("high");
</script>
<!-- Prebid Config Section END -->
Expected results
change of price granularity
Actual results
throwing setPriceGranularity is not a function
Platform details
0.23.1
Looks like that setPriceGranularity example isn't the best... it's loading prebid synchronously--which is a bad thing.
You're loading prebid async (which is good!)... but it also means that the prebid script hasn't run before your code tries to call setPriceGranularity.
That's why the que exists. You can use it to defer work until after prebid has loaded. Try this:
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
pbjs.que.push(function() {
pbjs.setPriceGranularity("high");
// Other pbjs calls go in here.
}
You need to wrap the pbjs function calls in the queue:
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
pbjs.que.push(function() {
pbjs.setPriceGranularity('high');
});
The prebid code isn't loaded before the browser tries to run the unwrapped function call. Once prebid's loaded, it runs through all the commands on the 'que'.
ahh, thank you. I moved it to the push block which I'm already using:
pbjs.que.push(function() {
pbjs.setPriceGranularity("high");
pbjs.addAdUnits(adUnits);
pbjs.requestBids({
bidsBackHandler: sendAdserverRequest
});
});