Skip to content

Commit

Permalink
Update Yandex & ADFOX amp-ad codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mishanga committed Jul 28, 2021
1 parent e20fe9a commit 05ce062
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 73 deletions.
6 changes: 4 additions & 2 deletions ads/_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ const adConfig = jsonConfiguration({
'adform': {},

'adfox': {
prefetch: 'https://yastatic.net/pcode/adfox/loader.js',
prefetch: 'https://yandex.ru/ads/system/context.js',
preconnect: ['https://yastatic.net/'],
renderStartImplemented: true,
},

Expand Down Expand Up @@ -1363,7 +1364,8 @@ const adConfig = jsonConfiguration({
},

'yandex': {
prefetch: 'https://an.yandex.ru/system/context_amp.js',
prefetch: 'https://yandex.ru/ads/system/context.js',
preconnect: ['https://yastatic.net/'],
renderStartImplemented: true,
},

Expand Down
43 changes: 8 additions & 35 deletions ads/vendors/adfox.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,65 +13,38 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {loadScript, validateData} from '#3p/3p';
import {yandex} from './yandex';

/**
* @param {!Window} global
* @param {!Object} data
*/
export function adfox(global, data) {
validateData(data, ['adfoxParams', 'ownerId']);
loadScript(global, 'https://yastatic.net/pcode/adfox/loader.js', () =>
initAdFox(global, data)
loadScript(global, 'https://yandex.ru/ads/system/context.js', () =>
renderAdFox(global, data)
);
}

/**
* @param {!Window} global
* @param {Object} data
* @param {!Object} data
*/
function initAdFox(global, data) {
const params = JSON.parse(data.adfoxParams);
function renderAdFox(global, data) {
const renderTo = 'adfox_container';

createContainer(global, 'adfox_container');
createContainer(global, renderTo);

global.Ya.adfoxCode.create({
ownerId: data.ownerId,
containerId: 'adfox_container',
params,
onLoad: (data, onRender, onError) => {
checkLoading(global, data, onRender, onError);
},
containerId: renderTo,
params: JSON.parse(data.adfoxParams),
onRender: () => global.context.renderStart(),
onError: () => global.context.noContentAvailable(),
onStub: () => global.context.noContentAvailable(),
});
}

/**
* @param {!Window} global
* @param {!Object} data
* @param {!Object} onRender
* @param {!Object} onError
* @return {boolean}
*/
function checkLoading(global, data, onRender, onError) {
if (data.bundleName === 'banner.direct') {
const dblParams = {
blockId: data.bundleParams.blockId,
data: data.bundleParams.data,
onRender,
onError,
};

yandex(global, dblParams);
return false;
}
return true;
}

/**
* @param {!Window} global
* @param {string} id
Expand Down
65 changes: 29 additions & 36 deletions ads/vendors/yandex.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,55 @@
*/
import {loadScript, validateData} from '#3p/3p';

const n = 'yandexContextAsyncCallbacks';
const renderTo = 'yandex_rtb';

/**
* @param {!Window} global
* @param {!Object} data
*/
export function yandex(global, data) {
validateData(data, ['blockId'], ['data', 'onRender', 'onError']);

addToQueue(global, data);
loadScript(global, 'https://an.yandex.ru/system/context_amp.js');
loadScript(global, 'https://yandex.ru/ads/system/context.js', () =>
renderYandex(global, data)
);
}

/**
* @param {!Window} global
* @param {!Object} data
*/
function addToQueue(global, data) {
global[n] = global[n] || [];
global[n].push(() => {
// Create container
createContainer(global, renderTo);
function renderYandex(global, data) {
const renderTo = 'yandex_rtb';

// Show Ad in container
global.Ya.Context.AdvManager.render(
{
blockId: data.blockId,
statId: data.statId,
renderTo,
data: data.data,
async: true,
onRender: () => {
if (typeof data.onRender === 'function') {
data.onRender();
}
global.context.renderStart();
},
},
() => {
if (typeof data.onError === 'function') {
data.onError();
} else {
global.context.noContentAvailable();
createContainer(global, renderTo);

global.Ya.Context.AdvManager.render(
{
blockId: data.blockId,
statId: data.statId,
renderTo,
data: data.data,
onRender: () => {
if (typeof data.onRender === 'function') {
data.onRender();
}
global.context.renderStart();
},
},
() => {
if (typeof data.onError === 'function') {
data.onError();
} else {
global.context.noContentAvailable();
}
);
});
}
);
}

/**
* @param {!Window} global
* @param {string} id
*/
function createContainer(global, id) {
const d = global.document.createElement('div');
d.id = id;
global.document.getElementById('c').appendChild(d);
const container = global.document.createElement('div');
container.setAttribute('id', id);
global.document.getElementById('c').appendChild(container);
}

0 comments on commit 05ce062

Please sign in to comment.