Skip to content

Commit

Permalink
Merge pull request #7 from Parrable/add-refresh-id-dev
Browse files Browse the repository at this point in the history
add refreshId concept to userId submodule
  • Loading branch information
icflournoy authored Sep 20, 2019
2 parents fa0019c + 30f5ce4 commit 53123b8
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
20 changes: 20 additions & 0 deletions modules/userId/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@
* @return {(Object|function)} id data or a callback, the callback is called on the auction end event
*/

/**
* @function
* @summary performs action to refresh stored id (only one of Submodule#getId or Submodule#refreshId will be called)
* @name Submodule#refreshId
* @param {SubmoduleParams} configParams
* @param {ConsentData} consentData
* @param {Object|string} currentStoredId
* @return {function} a callback, the callback is called on the auction end event
*/

/**
* @function
* @summary decode a stored value for passing to bid requests
Expand Down Expand Up @@ -343,6 +353,16 @@ function initSubmodules(submodules, consentData) {
submodule.idObj = submodule.submodule.decode(getIdResult);
}
}

if (utils.isFn(submodule.submodule.refreshId)) {
// if defined, refreshId will return a function that will load an updated id to be stored for subsequent use
const refreshIdResult = submodule.submodule.refreshId(submodule.config.params, consentData, storedId);

// a function to be called later is expected, otherwise ignore
if (utils.isFn(refreshIdResult)) {
submodule.callback = refreshIdResult;
}
}
} else if (submodule.config.value) {
// cache decoded value (this is copied to every adUnit bid)
submodule.idObj = submodule.config.value;
Expand Down
69 changes: 69 additions & 0 deletions test/spec/modules/userId_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,5 +582,74 @@ describe('User ID', function() {
done();
}, {adUnits});
});

it('should update id storage value if refreshId is defined', function(done) {
const conf = {
userSync: {
syncDelay: 0,
userIds: [{
name: 'mockId',
storage: {
expires: 365,
name: 'MOCKID',
type: 'cookie'
}
}]
}
};

const mockSubmodule = {
name: 'mockId',
decode: function(value) {
return {
'mid': value['MOCKID']
};
},
getId: function() {
return {'MOCKID': '1234'}
},
refreshId: function() {
const id = {'MOCKID': '5678'};
return function(cb) {
cb(id);
}
}
};

utils.setCookie('MOCKID', JSON.stringify({'MOCKID': '1234'}), new Date(Date.now() + 5000).toUTCString());
setSubmoduleRegistry([]);
init(config);
config.setConfig(conf);
attachIdSystem(mockSubmodule);

// first request
requestBidsHook(function() {
adUnits.forEach(unit => {
unit.bids.forEach(bid => {
// check MockId data was copied to bid
expect(bid).to.have.deep.nested.property('userId.mid');
expect(bid.userId.mid).to.equal('1234');
});
});

setSubmoduleRegistry([]);
init(config);
config.setConfig(conf);
attachIdSystem(mockSubmodule);

// second request, should get the updated value
requestBidsHook(function() {
adUnits.forEach(unit => {
unit.bids.forEach(bid => {
// check MockId data was copied to bid
expect(bid).to.have.deep.nested.property('userId.mid');
expect(bid.userId.mid).to.equal('5678');
});
});
utils.setCookie('MOCKID', '', EXPIRED_COOKIE_DATE);
done();
}, {adUnits});
}, {adUnits});
});
})
});

0 comments on commit 53123b8

Please sign in to comment.