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

OFF-465 Add getUserKey logic to prebid.js adapter #3

Merged
merged 3 commits into from
Apr 27, 2023
Merged
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
41 changes: 39 additions & 2 deletions modules/flippBidAdapter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {isEmpty, parseUrl} from '../src/utils.js';
import { registerBidder } from '../src/adapters/bidderFactory.js';
import { BANNER } from '../src/mediaTypes.js';
import {getStorageManager} from '../src/storageManager.js';

const NETWORK_ID = 11090;
const AD_TYPES = [4309, 641];
const DTX_TYPES = [5061];
Expand All @@ -11,6 +13,41 @@ const DEFAULT_TTL = 30;
const DEFAULT_CURRENCY = 'USD';
const DEFAULT_CREATIVE_TYPE = 'NativeX';
const VALID_CREATIVE_TYPES = ['DTX', 'NativeX'];
const FLIPP_USER_KEY = 'flipp-uid';

let userKey = null;
export const storage = getStorageManager({bidderCode: BIDDER_CODE});

export function getUserKey(options = {}) {
if (userKey) {
return userKey;
}

// If the partner provides the user key use it, otherwise fallback to cookies
if (options.userKey && isValidUserKey(options.userKey)) {
userKey = options.userKey;
return options.userKey;
}
// Grab from Cookie
const foundUserKey = storage.cookiesAreEnabled() && storage.getCookie(FLIPP_USER_KEY);
if (foundUserKey) {
return foundUserKey;
}

// Generate if none found
userKey = generateUUID();

// Set cookie
if (storage.cookiesAreEnabled()) {
storage.setCookie(FLIPP_USER_KEY, userKey);
}

return userKey;
}

function isValidUserKey(userKey) {
return !userKey.startsWith('#');
}

const generateUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
Expand Down Expand Up @@ -63,7 +100,7 @@ export const spec = {
buildRequests: function(validBidRequests, bidderRequest) {
const urlParams = parseUrl(bidderRequest.refererInfo.page).search;
const contentCode = urlParams['flipp-content-code'];
const userKey = isEmpty(validBidRequests[0]?.params.userKey) ? generateUUID() : validBidRequests[0]?.params.userKey;
const userKey = getUserKey(validBidRequests[0]?.params);
const placements = validBidRequests.map((bid, index) => {
return {
divName: TARGET_NAME,
Expand All @@ -76,11 +113,11 @@ export const spec = {
...(!isEmpty(contentCode) && {contentCode: contentCode.slice(0, 32)}),
},
prebid: {
creativeType: validateCreativeType(bid.params.creativeType),
requestId: bid.bidId,
publisherNameIdentifier: bid.params.publisherNameIdentifier,
height: bid.mediaTypes.banner.sizes[index][0],
width: bid.mediaTypes.banner.sizes[index][1],
creativeType: validateCreativeType(bid.params.creativeType),
}
}
});
Expand Down
1 change: 1 addition & 0 deletions modules/flippBidAdapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var adUnits = [
siteId: 1192075, // Required
zoneIds: [260678], // Optional
userKey: "", // Optional
creativeType: 'NativeX', // Optional
}
}
]
Expand Down