From cb55b0b0b4f57576763bc84bffa94df36327e950 Mon Sep 17 00:00:00 2001 From: affinity-admin <114151276+affinity-admin@users.noreply.github.com> Date: Sat, 22 Jul 2023 04:10:09 +0530 Subject: [PATCH] Adtype affinity v2 (#39287) * Chanegs and imporvements for ad vendor Affinity On branch adtype_affinity_v2 Changes to be committed: modified: ads/ads.extern.js modified: ads/vendors/affinity.js * Code linted by AMP cli utility On branch adtype_affinity_v2 Changes to be committed: modified: ads/ads.extern.js modified: ads/vendors/affinity.js --------- Co-authored-by: github-01-affinity --- ads/ads.extern.js | 8 ++- ads/vendors/affinity.js | 114 ++++++++++++++++++++++++++++++++++------ 2 files changed, 106 insertions(+), 16 deletions(-) diff --git a/ads/ads.extern.js b/ads/ads.extern.js index 3322e38357e55..159d14376444f 100644 --- a/ads/ads.extern.js +++ b/ads/ads.extern.js @@ -326,8 +326,14 @@ data.width; data.height; data.adtype; data.adslot; -data['multi-size']; +data.slot; +data.affLayout; +data.multiSize; data.jsontargeting; +data.affSticky; +data.affTitle; +data.affJson; +data.affRtcConfig; data.extras; // adreactor.js diff --git a/ads/vendors/affinity.js b/ads/vendors/affinity.js index 6f709731ba62e..28ff615d9e43c 100644 --- a/ads/vendors/affinity.js +++ b/ads/vendors/affinity.js @@ -7,22 +7,106 @@ import {loadScript, validateData} from '#3p/3p'; export function affinity(global, data) { validateData( data, - ['width', 'height', 'adtype', 'adslot'], - ['multi-size', 'jsontargeting', 'extras'] + ['width', 'height', 'adtype', ['adslot', 'slot']], + [ + 'affLayout', + 'multiSize', + 'affSticky', + 'affTitle', + 'affJson', + 'affRtcConfig', + 'jsontargeting', + 'extras', + ] ); - loadScript( - global, - 'https://securepubads.g.doubleclick.net/tag/js/gpt.js', - () => { - loadScript( - global, - 'https://cdn4-hbs.affinitymatrix.com/amp/v2022/amp.js', - () => { - (function () { - window.affinity.initAMP(global, data); - })(); + + const runV1 = function (g, d) { + if (false === g.isInitCalled) { + g.isInitCalled = true; + loadScript(g, gGPT, () => { + loadScript(g, affCDN + '/amp/v2022/amp.js', () => { + (function () { + window.affinity.initAMP(g, d); + })(); + }); + }); + } + }, + O2s = Object.prototype.toString, + isObject = function (val) { + return '[object Object]' == O2s.call(val); + }, + chkIsvalidContext = function (mixContext) { + if ('string' == typeof mixContext) { + try { + mixContext = JSON.parse(mixContext); + } catch (e) { + return false; } - ); + } + if (isObject(mixContext) && mixContext.sourceUrl) { + return true; + } + return false; + }, + getAmpContext = function () { + if (chkIsvalidContext(W.context)) { + return W.context; + } + if (chkIsvalidContext(W.AMP_CONTEXT_DATA)) { + return W.AMP_CONTEXT_DATA; + } + + return undefined; + }, + jsonParse = function (strJson) { + let ret = null; + try { + ret = JSON.parse(strJson); + } catch (e) { + try { + ret = JSON.parse(strJson.split("'").join('"')); + } catch (e2) {} + } + return ret; + }; + const W = global, + affCDN = 'https://cdn4-hbs.affinitymatrix.com', + gGPT = 'https://securepubads.g.doubleclick.net/tag/js/gpt.js', + extras = jsonParse(String(data.extras)); + + global.affinity = global.affinity || {task: []}; + global.isInitCalled = false; + global.affinity.initAMP = function (global, data) { + if (global.isInitCalled == true) { + return; } - ); + global.isInitCalled = true; + try { + const dtObj = new Date(); + const cb = String(dtObj.getDate()) + dtObj.getMonth() + dtObj.getHours(); + const dmn = extras.d.replace('www.', ''); + + const cfgData = {...data, ...extras}; + + const libUrl = affCDN + '/amplib/' + dmn + '/a' + cb + '/amp.php?t=' + cb; + loadScript(global, libUrl); + global.affinity.task.push(function () { + global.affinity.init(cfgData); + }); + } catch (e) { + runV1(global, data); + } + }; + try { + if (getAmpContext() !== undefined) { + if (extras && extras.ver && extras.ver == 1) { + // console.log('extras.ver == 1'); + global.affinity.initAMP(global, data); + data.adStatus = Date.now(); + return; + } + } + } catch (e) {} + runV1(global, data); }