Skip to content

Commit

Permalink
Fix 'metrika-yandex-tag' — scriptlet does not work if request is bloc…
Browse files Browse the repository at this point in the history
…ked by DNS. #472

Squashed commit of the following:

commit ecfc4ec
Author: Adam Wróblewski <[email protected]>
Date:   Mon Dec 23 20:52:34 2024 +0100

    Fix metrika-yandex-tag scriptlet
  • Loading branch information
AdamWr committed Dec 26, 2024
1 parent 103221e commit 38ee6e1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic

### Fixed

- issue with `metrika-yandex-tag` redirect when it's used as a scriptlet [#472]
- issue with `trusted-click-element` scriptlet when `delay` was used and the element was removed
and added again before it was clicked [#391]

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.0.1...HEAD
[#391]: https://github.com/AdguardTeam/Scriptlets/issues/391
[#468]: https://github.com/AdguardTeam/Scriptlets/issues/468
[#472]: https://github.com/AdguardTeam/Scriptlets/issues/472

## [v2.0.1] - 2024-11-13

Expand Down
11 changes: 7 additions & 4 deletions src/redirects/metrika-yandex-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,19 @@ export function metrikaYandexTag(source) {
destruct,
};

function ym(id, funcName, ...args) {
return api[funcName] && api[funcName](id, ...args);
}

function init(id) {
// yaCounter object should provide api
window[`yaCounter${id}`] = api;
document.dispatchEvent(new Event(`yacounter${id}inited`));
}

function ym(id, funcName, ...args) {
if (funcName === 'init') {
return init(id);
}
return api[funcName] && api[funcName](id, ...args);
}

if (typeof window.ym === 'undefined') {
window.ym = ym;
ym.a = [];
Expand Down
34 changes: 34 additions & 0 deletions tests/redirects/metrika-yandex-tag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,40 @@ test('API mocking test', (assert) => {
clearGlobalProps(`yaCounter${counterId1}`, `yaCounter${counterId1}`);
});

test('Init mocking test - when it is used as a scriptlet', (assert) => {
let testPassed = false;

evalWrapper(redirects.getRedirect(name).content);

window.ym = window.ym || function YandexMetrika(...args) {
(window.ym.a = window.ym.a || []).push(...args);
};

const counterId = 28510826;

window.ym(counterId, 'init', {
clickmap: true,
trackLinks: true,
accurateTrackBounce: true,
webvisor: true,
ecommerce: 'dataLayer',
});

try {
window.yaCounter28510826.reachGoal('login');
testPassed = true;
} catch (error) {
// eslint-disable-next-line no-console
console.error(`yaCounter${counterId} is not defined:`, error);
}

assert.ok(window.ym, 'Metrika function was created');
assert.ok(typeof window[`yaCounter${counterId}`] === 'object', 'yaCounter was created');
assert.strictEqual(testPassed, true, 'testPassed is true');

clearGlobalProps(`yaCounter${counterId}`);
});

test('ym: API methods test', (assert) => {
assert.expect(4);

Expand Down

0 comments on commit 38ee6e1

Please sign in to comment.