Skip to content

Commit

Permalink
Bugfix/user sync setconfig (#1615)
Browse files Browse the repository at this point in the history
* usersync should register configs set with config.setConfig()

* do not overwrite user-sync config

* fixed lint error
  • Loading branch information
harpere authored Sep 22, 2017
1 parent 63a8469 commit 4349563
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
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');
});
});

0 comments on commit 4349563

Please sign in to comment.