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 1 commit
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
63 changes: 60 additions & 3 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 {isEmpty, parseUrl, triggerPixel} 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 @@ -12,6 +14,61 @@ const DEFAULT_CURRENCY = 'USD';
const DEFAULT_CREATIVE_TYPE = 'NativeX';
const VALID_CREATIVE_TYPES = ['DTX', 'NativeX'];

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

const syncUserKey = (userKey) => {
if (!cookieSynced) {
triggerPixel(`https://idsync.rlcdn.com/712559.gif?partner_uid=${userKey}`);
cookieSynced = true;
}
};

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

// If the partner provides the user key use it, otherwise fallback to cookies
if ('userKey' in options && options.userKey) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ('userKey' in options && options.userKey) {
if (options.userKey) {

if (isValidUserKey(options.userKey)) {
userKey = options.userKey;
syncUserKey(userKey);
return options.userKey;
}
}

const FLIPP_USER_KEY = 'flipp-uid';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put all constants on the top


// Grab from Cookie
const foundUserKey = storage.cookiesAreEnabled() && storage.getCookie(FLIPP_USER_KEY);
if (foundUserKey) {
syncUserKey(foundUserKey);
return foundUserKey;
}

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

if (!userKey) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that it will be some empty value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to have empty user key if everything fails, kevel request would still work without a key
Updated the branch to return empty string instead of null anyway

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps I haven't described well, I meant in general, do we need this check rather then what's the default value of this condition.
Looks like generateUUID always returns string, that's was my question.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, just removed the check

return null;
}

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

syncUserKey(userKey);
return userKey;
}

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

const generateUUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
const r = (Math.random() * 16) | 0;
Expand Down Expand Up @@ -63,7 +120,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 +133,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