Skip to content

Commit 38ee6e1

Browse files
committed
Fix 'metrika-yandex-tag' — scriptlet does not work if request is blocked by DNS. #472
Squashed commit of the following: commit ecfc4ec Author: Adam Wróblewski <adam@adguard.com> Date: Mon Dec 23 20:52:34 2024 +0100 Fix metrika-yandex-tag scriptlet
1 parent 103221e commit 38ee6e1

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
2020

2121
### Fixed
2222

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

2627
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.0.1...HEAD
2728
[#391]: https://github.com/AdguardTeam/Scriptlets/issues/391
2829
[#468]: https://github.com/AdguardTeam/Scriptlets/issues/468
30+
[#472]: https://github.com/AdguardTeam/Scriptlets/issues/472
2931

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

src/redirects/metrika-yandex-tag.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,19 @@ export function metrikaYandexTag(source) {
108108
destruct,
109109
};
110110

111-
function ym(id, funcName, ...args) {
112-
return api[funcName] && api[funcName](id, ...args);
113-
}
114-
115111
function init(id) {
116112
// yaCounter object should provide api
117113
window[`yaCounter${id}`] = api;
118114
document.dispatchEvent(new Event(`yacounter${id}inited`));
119115
}
120116

117+
function ym(id, funcName, ...args) {
118+
if (funcName === 'init') {
119+
return init(id);
120+
}
121+
return api[funcName] && api[funcName](id, ...args);
122+
}
123+
121124
if (typeof window.ym === 'undefined') {
122125
window.ym = ym;
123126
ym.a = [];

tests/redirects/metrika-yandex-tag.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@ test('API mocking test', (assert) => {
4242
clearGlobalProps(`yaCounter${counterId1}`, `yaCounter${counterId1}`);
4343
});
4444

45+
test('Init mocking test - when it is used as a scriptlet', (assert) => {
46+
let testPassed = false;
47+
48+
evalWrapper(redirects.getRedirect(name).content);
49+
50+
window.ym = window.ym || function YandexMetrika(...args) {
51+
(window.ym.a = window.ym.a || []).push(...args);
52+
};
53+
54+
const counterId = 28510826;
55+
56+
window.ym(counterId, 'init', {
57+
clickmap: true,
58+
trackLinks: true,
59+
accurateTrackBounce: true,
60+
webvisor: true,
61+
ecommerce: 'dataLayer',
62+
});
63+
64+
try {
65+
window.yaCounter28510826.reachGoal('login');
66+
testPassed = true;
67+
} catch (error) {
68+
// eslint-disable-next-line no-console
69+
console.error(`yaCounter${counterId} is not defined:`, error);
70+
}
71+
72+
assert.ok(window.ym, 'Metrika function was created');
73+
assert.ok(typeof window[`yaCounter${counterId}`] === 'object', 'yaCounter was created');
74+
assert.strictEqual(testPassed, true, 'testPassed is true');
75+
76+
clearGlobalProps(`yaCounter${counterId}`);
77+
});
78+
4579
test('ym: API methods test', (assert) => {
4680
assert.expect(4);
4781

0 commit comments

Comments
 (0)