Skip to content

Commit

Permalink
Adtype affinity v2 (#39287)
Browse files Browse the repository at this point in the history
* 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 <github-01@affinity.com>
  • Loading branch information
affinity-admin and github-01-affinity authored Jul 21, 2023
1 parent 17072aa commit 6a27fa8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 16 deletions.
8 changes: 7 additions & 1 deletion ads/ads.extern.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
114 changes: 99 additions & 15 deletions ads/vendors/affinity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 6a27fa8

Please sign in to comment.