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

Browsi Moduleで開催されたオークションでPrebid Analyticsを送信できるようにする #4

Merged
merged 7 commits into from
Sep 30, 2021
38 changes: 26 additions & 12 deletions modules/fluctAnalyticsAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ const isBrowsiAuction = (auctionId) => {
const siteKey = find(pbjs.getConfig().realTimeData?.dataProviders ?? [], provider => provider.name === 'browsi')?.params.siteKey
return Boolean(auctionId.match(new RegExp(`^${siteKey}`, 'g')))
}
/**
* @param {string} divId
* @returns {boolean}
*/
const isBrowsiDivId = (divId) => Boolean(divId.match(/^browsi_ad_/g))

/* eslint-disable-next-line compat/compat */
let fluctAnalyticsAdapter = Object.assign(
Expand Down Expand Up @@ -189,6 +194,17 @@ let fluctAnalyticsAdapter = Object.assign(
}
break;
}
// 全てnobid時発火しない
case CONSTANTS.EVENTS.SET_TARGETING: {
Comment on lines +197 to +198
Copy link
Author

Choose a reason for hiding this comment

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

browsiImpression 実装までの繋ぎ

SET_TARGETING browsi枠がGPTで定義された直後のタイミング
AUCTION_END 時は未定義のため、analytics送信を遅らせる

let setTargetingEvent = args
/** @type {Array<String>} */
let divIds = Object.keys(setTargetingEvent)
if (divIds.every(isBrowsiDivId)) {
let auctionId = find(Object.values(cache.auctions), auction => find(auction.adUnits, adUnit => divIds.includes(adUnit.code)))?.auctionId
Copy link

Choose a reason for hiding this comment

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

📝 setTargetingされた枠のdivIdからauctionIdを引いている

sendMessage(auctionId)
Copy link

Choose a reason for hiding this comment

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

AUCTION_ENDイベント時に送信しなかった分をこっちで送っている

}
break;
}
case CONSTANTS.EVENTS.BID_WON: {
/** @type {Bid} */
let bidWonEvent = args
Expand All @@ -200,11 +216,9 @@ let fluctAnalyticsAdapter = Object.assign(
bidWon: true,
timeout: false,
})
if (!isBrowsiAuction(auctionId)) {
cache.timeouts[auctionId] = setTimeout(() => {
sendMessage(auctionId);
}, pbjs.getConfig().bidderTimeout || 3000);
}
cache.timeouts[auctionId] = setTimeout(() => {
sendMessage(auctionId);
}, pbjs.getConfig().bidderTimeout || 3000);
break;
}
default:
Expand Down Expand Up @@ -233,15 +247,15 @@ export const getAdUnitCodeBeforeReplication = (slots, adUnitCode) => {
return slot.code === adUnitCode
|| slot.code.match(new RegExp(`^${browsiPrefix}`, 'g'))?.[0]
})
return find(slots, _slot => _slot.path === slot.path && !_slot.code.match(/^browsi_/g))?.code ?? adUnitCode
return find(slots, _slot => _slot.path === slot.path && !isBrowsiDivId(_slot.code))?.code ?? adUnitCode
}

/**
* @param {AdUnit} adUnit
* @returns {AdUnit}
*/
const modifyBrowsiAdUnit = (adUnit) => {
if (!adUnit.code.match(/^browsi_/g)) return adUnit
if (!isBrowsiDivId(adUnit.code)) return adUnit
// e.g.
// [
// [{ code: 'div-gpt-ad-1629864618640-0', path: '/62532913/p_fluctmagazine_320x50_surface_15377' }]
Expand Down Expand Up @@ -272,7 +286,7 @@ const modifyBrowsiAuctionId = (auctionId, adUnits) => {
/** @type {string|undefined} */
const siteKey = find(pbjs.getConfig().realTimeData?.dataProviders ?? [], provider => provider.name === 'browsi')?.params.siteKey
/** @type {string|undefined} */
const reloadCount = getBrowsiRefreshCount(find(adUnits, adUnit => adUnit.code.match(/^browsi_/g))?.code)
const reloadCount = getBrowsiRefreshCount(find(adUnits, adUnit => isBrowsiDivId(adUnit.code))?.code)
return auctionId.match(new RegExp(`^${siteKey}`, 'g')) && reloadCount
? `${auctionId}_${reloadCount}`
: auctionId
Expand Down Expand Up @@ -333,10 +347,10 @@ const sendMessage = (auctionId) => {
ajax(url, () => utils.logInfo(`[sendMessage] ${Date.now()} :`, payload), JSON.stringify(payload), { contentType: 'application/json', method: 'POST' });
};

window.addEventListener('browsiImpression', (data) => {
const auction = find(Object.values(cache.auctions), auction => auction.adUnitCodes.includes(data.detail.adUnit.code))
sendMessage(auction.auctionId)
})
// window.addEventListener('browsiImpression', (data) => {
// const auction = find(Object.values(cache.auctions), auction => auction.adUnitCodes.includes(data.detail.adUnit.code))
// sendMessage(auction.auctionId)
// })
Comment on lines +344 to +347
Copy link
Author

Choose a reason for hiding this comment

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

browsiImpression: 実装待ちにつき一旦利用しない方向


fluctAnalyticsAdapter.originEnableAnalytics = fluctAnalyticsAdapter.enableAnalytics;
fluctAnalyticsAdapter.enableAnalytics = (config) => {
Expand Down