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

Bugfix/user sync setconfig #1615

Merged
merged 4 commits into from
Sep 22, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
22 changes: 13 additions & 9 deletions src/userSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export function newUserSync(userSyncDependencies) {
let numAdapterBids = {};

// Use what is in config by default
const config = userSyncDependencies.config;
let usConfig = userSyncDependencies.config;
// Update if it's (re)set
config.getConfig('userSync', (conf) => {
usConfig = Object.assign(usConfig, conf.userSync);
});

/**
* @function getDefaultQueue
Expand All @@ -40,7 +44,7 @@ export function newUserSync(userSyncDependencies) {
* @private
*/
function fireSyncs() {
if (!config.syncEnabled || !userSyncDependencies.browserSupportsCookies || hasFired) {
if (!usConfig.syncEnabled || !userSyncDependencies.browserSupportsCookies || hasFired) {
return;
}

Expand All @@ -63,7 +67,7 @@ export function newUserSync(userSyncDependencies) {
* @private
*/
function fireImagePixels() {
if (!config.pixelEnabled) {
if (!usConfig.pixelEnabled) {
return;
}
// Randomize the order of the pixels before firing
Expand All @@ -83,7 +87,7 @@ export function newUserSync(userSyncDependencies) {
* @private
*/
function loadIframes() {
if (!config.iframeEnabled) {
if (!usConfig.iframeEnabled) {
return;
}
// Randomize the order of these syncs just like the pixels above
Expand Down Expand Up @@ -125,18 +129,18 @@ export function newUserSync(userSyncDependencies) {
* userSync.registerSync('image', 'rubicon', 'http://example.com/pixel')
*/
publicApi.registerSync = (type, bidder, url) => {
if (!config.syncEnabled || !utils.isArray(queue[type])) {
if (!usConfig.syncEnabled || !utils.isArray(queue[type])) {
return utils.logWarn(`User sync type "{$type}" not supported`);
}
if (!bidder) {
return utils.logWarn(`Bidder is required for registering sync`);
}
if (Number(numAdapterBids[bidder]) >= config.syncsPerBidder) {
if (Number(numAdapterBids[bidder]) >= usConfig.syncsPerBidder) {
return utils.logWarn(`Number of user syncs exceeded for "{$bidder}"`);
}
// All bidders are enabled by default. If specified only register for enabled bidders.
let hasEnabledBidders = config.enabledBidders && config.enabledBidders.length;
if (hasEnabledBidders && config.enabledBidders.indexOf(bidder) < 0) {
let hasEnabledBidders = usConfig.enabledBidders && usConfig.enabledBidders.length;
if (hasEnabledBidders && usConfig.enabledBidders.indexOf(bidder) < 0) {
return utils.logWarn(`Bidder "${bidder}" not supported`);
}
queue[type].push([bidder, url]);
Expand All @@ -162,7 +166,7 @@ export function newUserSync(userSyncDependencies) {
* @public
*/
publicApi.triggerUserSyncs = () => {
if (config.enableOverride) {
if (usConfig.enableOverride) {
publicApi.syncUsers();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ exports.triggerPixel = function (url) {
* @param {string} encodeUri boolean if URL should be encoded before inserted. Defaults to true
*/
exports.insertUserSyncIframe = function(url) {
let iframeHtml = this.createTrackPixelIframeHtml(url, false, 'allow-scripts');
let iframeHtml = this.createTrackPixelIframeHtml(url, false, 'allow-scripts allow-same-origin');
let div = document.createElement('div');
div.innerHTML = iframeHtml;
let iframe = div.firstChild;
Expand Down
11 changes: 11 additions & 0 deletions test/spec/userSync_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,15 @@ describe('user sync', () => {
expect(triggerPixelStub.getCall(0).args[0]).to.exist.and.to.include('http://example.com/');
expect(triggerPixelStub.getCall(1)).to.be.null;
});

it('should register config set after instantiation', () => {
// start with userSync off
const userSync = newTestUserSync({syncEnabled: false});
// turn it on with setConfig()
config.setConfig({userSync: {syncEnabled: true}});
userSync.registerSync('image', 'testBidder', 'http://example.com');
userSync.syncUsers();
expect(triggerPixelStub.getCall(0)).to.not.be.null;
expect(triggerPixelStub.getCall(0).args[0]).to.exist.and.to.equal('http://example.com');
});
});