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

New User ID Submodule: Rewarded Interest #12340

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
114 changes: 114 additions & 0 deletions integrationExamples/gpt/rewardedInterestIdSystem_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Rewarded Interest ID Example</title>
<script>
const FAILSAFE_TIMEOUT = 2000;
const adUnits = [
{
code: 'test-div',
mediaTypes: {
banner: {
sizes: [[300, 250], [300, 600], [728, 90]]
},
},
bids: [
{
bidder: 'rubicon',
params: {
accountId: '1001',
siteId: '113932',
zoneId: '535510'
}
}
]
}
];
var pbjs = pbjs || {};
pbjs.que = pbjs.que || [];
</script>
<script src="../../build/dev/prebid.js" async></script>

<script>
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
googletag.cmd.push(function () {
googletag.pubads().disableInitialLoad();
});

pbjs.que.push(function () {
pbjs.setConfig({
debug: true,
userSync: {
userIds: [
{
name: 'rewardedInterestId',
},
],
syncDelay: 5000,
auctionDelay: 1000,
}
});
pbjs.addAdUnits(adUnits);
pbjs.requestBids({
bidsBackHandler: sendAdserverRequest
});
});

function sendAdserverRequest() {
if (pbjs.adserverRequestSent) return;
pbjs.adserverRequestSent = true;
googletag.cmd.push(function () {
pbjs.que.push(function () {
pbjs.setTargetingForGPTAsync();
googletag.pubads().refresh();
});
});
}

setTimeout(function () {
sendAdserverRequest();
}, FAILSAFE_TIMEOUT);
</script>

<script>
(function () {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
gads.src = 'https://securepubads.g.doubleclick.net/tag/js/gpt.js';
var node = document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
</script>

<script>
googletag.cmd.push(function () {
googletag.defineSlot('/112115922/FL_PB_MedRect', [[300, 250], [300, 600], [728, 90]], 'test-div').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.enableServices();
});
</script>
</head>

<body>
<script>
pbjs.que.push(function () {
pbjs.getUserIdsAsync().then(ids => {
document.getElementById('ids-div').innerHTML = JSON.stringify(ids, null, ' ');
document.getElementById('eids-div').innerHTML = JSON.stringify(pbjs.getUserIdsAsEids(), null, ' ');
});
});
</script>

<h2>Rewarded Interest ID Example</h2>

<h4>Generated IDs:</h4>
<pre id="ids-div" style="border:1px solid #333; padding:5px; overflow: auto"></pre>

<h4>Generated EIDs</h4>
<pre id="eids-div" style="border:1px solid #333; padding:5px; overflow: auto"></pre>
</body>

</html>
1 change: 1 addition & 0 deletions modules/.submodules.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"pubProvidedIdSystem",
"publinkIdSystem",
"quantcastIdSystem",
"rewardedInterestIdSystem",
"sharedIdSystem",
"tapadIdSystem",
"teadsIdSystem",
Expand Down
142 changes: 142 additions & 0 deletions modules/rewardedInterestIdSystem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/**
* This module adds rewarded interest ID to the User ID module
* The {@link module:modules/userId} module is required
* @module modules/rewardedInterestIdSystem
* @requires module:modules/userId
*/

/**
* @typedef {import('../modules/userId/index.js').Submodule} Submodule
* @typedef {import('../modules/userId/index.js').IdResponse} IdResponse
*/

/**
* @typedef RewardedInterestApi
* @property {getApiVersion} getApiVersion
* @property {getIdentityToken} getIdentityToken
*/

/**
* Retrieves the Rewarded Interest API version.
* @callback getApiVersion
* @return {string}
*/

/**
* Retrieves the current identity token.
* @callback getIdentityToken
* @return {Promise<string>}
*/

import {submodule} from '../src/hook.js';
import {logError} from '../src/utils.js';

export const MODULE_NAME = 'rewardedInterestId';
export const SOURCE = 'rewardedinterest.com';

/**
* Get rewarded interest API
* @function
* @returns {RewardedInterestApi|undefined}
*/
export function getRewardedInterestApi() {
if (window.__riApi && window.__riApi.getIdentityToken) {
return window.__riApi;
}
}

/**
* Wait while rewarded interest API to be set and execute the callback function
* @param {function} callback
*/
export function watchRewardedInterestApi(callback) {
Object.defineProperties(window, {
__rewardedInterestApi: {
value: undefined,
writable: true
},
__riApi: {
get: () => {
return window.__rewardedInterestApi;
},
set: value => {
window.__rewardedInterestApi = value;
callback(value);
},
configurable: true,
}
});
}

/**
* Get rewarded interest ID from API and pass it to the callback function
* @param {RewardedInterestApi} rewardedInterestApi
* @param {function} callback User ID callbackCompleted
*/
export function getRewardedInterestId(rewardedInterestApi, callback) {
rewardedInterestApi.getIdentityToken().then(callback).catch(error => {
callback();
logError(`${MODULE_NAME} module: ID fetch encountered an error`, error);
});
}

/**
* @param {function} callback User ID callbackCompleted
*/
export function apiNotAvailable(callback) {
callback();
logError(`${MODULE_NAME} module: Rewarded Interest API not found`);
}

/** @type {Submodule} */
export const rewardedInterestIdSubmodule = {
/**
* Used to link submodule with config
* @type {string}
*/
name: MODULE_NAME,

/**
* Decode the stored id value for passing to bid requests
* @function
* @param {string} value
* @returns {{rewardedInterestId: string}|undefined}
*/
decode(value) {
return value ? {[MODULE_NAME]: value} : undefined;
},

/**
* Performs action to obtain id and return a value in the callback's response argument
* @function
* @returns {IdResponse|undefined}
*/
getId() {
return {
callback: cb => {
const api = getRewardedInterestApi();
if (api) {
getRewardedInterestId(api, cb);
} else if (document.readyState === 'complete') {
apiNotAvailable(cb);
} else {
watchRewardedInterestApi(api => getRewardedInterestId(api, cb));
// Ensure that cb is called when API is not available
window.addEventListener('load', () => {
if (!getRewardedInterestApi()) {
apiNotAvailable(cb);
}
})
}
},
};
},
eids: {
[MODULE_NAME]: {
source: SOURCE,
atype: 3,
},
},
};

submodule('userId', rewardedInterestIdSubmodule);
23 changes: 23 additions & 0 deletions modules/rewardedInterestIdSystem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Rewarded Interest User ID Submodule

This module adds rewarded interest advertising token to the user ID module

*Note: The storage config should be omitted

### Prebid Params

```javascript
pbjs.setConfig({
userSync: {
userIds: [{
name: 'rewardedInterestId',
}]
}
});
```

## Parameter Descriptions for the `usersync` Configuration Section

| Param under usersync.userIds[] | Scope | Type | Description | Example |
|--------------------------------|----------|--------|--------------------------|------------------------|
| name | Required | String | The name of this module. | `"rewardedInterestId"` |
Loading