Skip to content

Commit

Permalink
improve metrika-yandex-tag redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Mar 10, 2022
1 parent c0904a5 commit 38b2ff0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/redirects/metrika-yandex-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@ export function metrikaYandexTag(source) {
function init(id) {
// yaCounter object should provide api
window[`yaCounter${id}`] = api;
document.dispatchEvent(new Event(`yacounter${id}inited`));
}

if (typeof window.ym === 'undefined') {
window.ym = ym;
} else if (window.ym && window.ym.a) {
// Get id for yaCounter object
window.ym.a.forEach((params) => {
const counters = window.ym.a;

window.ym = ym;
counters.forEach((params) => {
const id = params[0];
init(id);
});
window.ym = ym;
}

hit(source);
}

Expand Down
8 changes: 7 additions & 1 deletion tests/redirects/metrika-yandex-tag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,15 @@ test('ym: API methods test', (assert) => {
});

test('yaCounter: API methods test', (assert) => {
assert.expect(6);
assert.expect(7);

const id = 111;
window.ym = () => {};
window.ym.a = [[id]];
const eventHandler = () => {
assert.ok(true, 'Counter event dispatched');
};
document.addEventListener(`yacounter${id}inited`, eventHandler);

runRedirect(name);

Expand Down Expand Up @@ -125,5 +129,7 @@ test('yaCounter: API methods test', (assert) => {
yaCounter.reachGoal(1, 'target', 'params', reachGoalCb, 123);

assert.strictEqual(window.hit, 'FIRED', 'hit function was executed');

document.removeEventListener(`yacounter${id}inited`, eventHandler);
clearGlobalProps(`yaCounter${id}`);
});

0 comments on commit 38b2ff0

Please sign in to comment.