Currently, I am in the process of setting up header bidding with Prebid. My approach involves loading the desired ads from a database using PHP and MySQL, followed by creating text variables in JavaScript with the PHP data.
The issue arises when I attempt to create the "adUnits" variable. While manually writing the content directly works fine, replicating the same process using JavaScript results in failure.
Here is the code that functions correctly:
var adUnits = [{
code: '/123/468x60',
mediaTypes: {
banner: {
sizes: [
[468, 60]
]
}
},
bids: [{
bidder: 'adserver',
params: {
networkId: 1234
}
}]
}, {
code: '/123/336x280',
mediaTypes: {
banner: {
sizes: [
[336, 280],
[300, 250]
]
}
},
bids: [{
bidder: 'adserver',
params: {
networkId: 1234
}
}]
}, {
code: '/123/300x600',
mediaTypes: {
banner: {
sizes: [
[300, 600],
[300, 250]
]
}
},
bids: [{
bidder: 'adserver',
params: {
networkId: 1234
}
}]
}];
And here is the code that isn't working:
var arrayadsname = adsname.split('#'); // Content of arrayadsname => /123/468x60#/123/336x280#/123/300x600
var arrayadssize = adssize.split('#'); // Content of arrayadssize => 468x60#336,280|300,250#300,600|300,250
var arraysize = '';
var ad = '';
var adUnits = [];
var preadUnits = '';
for (var i = 0; i < arrayadsname.length; i++) {
var arraysizeparts = arrayadssize[i].split('|');
for (var j = 0; j < arraysizeparts.length; j++) {
if (j == 0) {
arraysize = '[' + arraysizeparts[j] + ']';
} else {
arraysize = arraysize + ',' + '[' + arraysizeparts[j] + ']';
}
}
ad = '{code: \'' + arrayadsname[i] + '\', mediaTypes: {banner: {sizes: ' + '[' + arraysize + ']' + '}}, bids: [{bidder: \'adserver\', params: {networkId: 1234}}]}';
if (i == 0) preadUnits = ad;
else preadUnits = preadUnits + ',' + ad;
}
// Result of ad => {code: '/123/468x60', mediaTypes: {banner: {sizes: [[468,60]] }}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]}, {code: '/123/336x280', mediaTypes: {banner: {sizes: [[336,280],[300,250]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]}, {code: '/123/300x600', mediaTypes: {banner: {sizes: [[300,600],[300,250]]}}, bids: [{bidder: 'adserver', params: {networkId: 1234}}]}
adUnits = "[" + JSON.stringify(preadUnits) + "]";
I have attempted various combinations by declaring the variable adUnits as an object, array, and text, utilizing JSON.stringify, JSON.parse, and also declaring "arraysize" as an array and parsing it. However, I am unable to identify the root cause of the issue.
Upon checking the console, I noticed this error: "callee: [Exception: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them at Arguments.invokeGetter".
Thank you in advance.