### GTM Tag Setup and Verification
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
Instructions for setting up the Yandex Webmaster tag in GTM, including recommended trigger settings and a JavaScript snippet to verify the meta tag after publishing.
```javascript
Array.from(document.head.querySelectorAll('meta[name="yandex-verification"]'))
.map(function(m) { return m.outerHTML; });
```
--------------------------------
### Download and Import Yandex Webmaster GTM Template
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
Instructions for manually downloading the template.tpl file and importing it into Google Tag Manager. Ensure the helper script URL in the .tpl file is synchronized with the inject_script permission and built-in tests.
```bash
# Download the template file
curl -O https://raw.githubusercontent.com/yandex/webmaster-gtm-template/main/template.tpl
# In GTM:
# 1. Templates → Tag Templates → New → ⋮ menu → Import
# 2. Select the downloaded template.tpl
# 3. Save, then create a tag from the imported template
# 4. Follow the same trigger/firing configuration as the Gallery install
# Verify the pinned helper URL in the .tpl matches the inject_script permission:
grep 'HELPER_SCRIPT_URL' template.tpl
# https://cdn.jsdelivr.net/gh/yandex/webmaster-gtm-template@cc8bead2195157cf02650a02485ef44b1fa4a0c1/webmaster-verification.js
```
--------------------------------
### webmaster-verification.js Helper Script
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
This IIFE script adds Yandex verification meta tags to the document's head. It reads tokens from `window.yandexWebmasterVerification` and prevents duplicates by checking existing meta tags. Load this script via jsDelivr.
```javascript
(function (window, document) {
'use strict';
var config = window.yandexWebmasterVerification || {};
var head = document.head || document.getElementsByTagName('head')[0];
if (!head) return;
// Returns true if a already exists
function hasMetaWithToken(token) {
var metas = head.querySelectorAll('meta[name="yandex-verification"]');
for (var i = 0; i < metas.length; i += 1) {
if (metas[i].getAttribute('content') === token) return true;
}
return false;
}
// Creates and appends the meta element, skipping empty or duplicate tokens
function addVerificationMeta(token) {
token = typeof token === 'string' ? token.replace(/^\s+|\s+$/g, '') : '';
if (!token || hasMetaWithToken(token)) return;
var meta = document.createElement('meta');
meta.setAttribute('name', 'yandex-verification');
meta.setAttribute('content', token);
head.appendChild(meta);
}
// Collects tokens from both current format { tokens: [...] } and legacy { token: '...' }
function collectTokens(cfg) {
var tokens = [];
if (cfg && Object.prototype.toString.call(cfg.tokens) === '[object Array]') {
for (var i = 0; i < cfg.tokens.length; i += 1) tokens.push(cfg.tokens[i]);
}
if (cfg && typeof cfg.token === 'string') tokens.push(cfg.token);
return tokens;
}
collectTokens(config).forEach(function(token) {
addVerificationMeta(token);
});
})(window, document);
```
--------------------------------
### GTM Sandbox Permissions Configuration
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
Defines the GTM sandbox permissions required for the Yandex Webmaster template. It grants read/write access to `yandexWebmasterVerification` and whitelists a specific jsDelivr URL for script injection.
```json
[
{
"instance": {
"key": { "publicId": "access_globals", "versionId": "1" },
"param": [{
"key": "keys",
"value": {
"type": 2,
"listItem": [{
"type": 3,
"mapKey": [
{ "type": 1, "string": "key" },
{ "type": 1, "string": "read" },
{ "type": 1, "string": "write" },
{ "type": 1, "string": "execute" }
],
"mapValue": [
{ "type": 1, "string": "yandexWebmasterVerification" },
{ "type": 8, "boolean": true },
{ "type": 8, "boolean": true },
{ "type": 8, "boolean": false }
]
}]
}
}]
}
},
{
"instance": {
"key": { "publicId": "inject_script", "versionId": "1" },
"param": [{
"key": "urls",
"value": {
"type": 2,
"listItem": [{
"type": 1,
"string": "https://cdn.jsdelivr.net/gh/yandex/webmaster-gtm-template@cc8bead2195157cf02650a02485ef44b1fa4a0c1/webmaster-verification.js"
}]
}
}]
}
}
]
```
--------------------------------
### Sandboxed Tag Logic for Web Template
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
This JavaScript code handles the core logic of the GTM template. It checks for necessary GTM permissions, accumulates and deduplicates verification IDs, and injects a helper script. Ensure 'access_globals' and 'inject_script' permissions are granted.
```javascript
// Excerpt from template.tpl ___SANDBOXED_JS_FOR_WEB_TEMPLATE___
const injectScript = require('injectScript');
const makeString = require('makeString');
const queryPermission = require('queryPermission');
const setInWindow = require('setInWindow');
const copyFromWindow = require('copyFromWindow');
const getType = require('getType');
const GLOBAL_NAMESPACE = 'yandexWebmasterVerification';
const HELPER_SCRIPT_URL = 'https://cdn.jsdelivr.net/gh/yandex/webmaster-gtm-template@cc8bead2195157cf02650a02485ef44b1fa4a0c1/webmaster-verification.js';
const verificationId = makeString(data.verificationId || '').trim();
// Guard: empty ID
if (!verificationId) {
data.gtmOnFailure();
return;
}
// Guard: permissions
if (!queryPermission('access_globals', 'readwrite', GLOBAL_NAMESPACE) ||
!queryPermission('inject_script', HELPER_SCRIPT_URL)) {
data.gtmOnFailure();
return;
}
// Accumulate tokens from any previous tag firings on the same page
const currentConfig = copyFromWindow(GLOBAL_NAMESPACE) || {};
const tokens = [];
const containsToken = function(token) {
for (let i = 0; i < tokens.length; i++) {
if (tokens[i] === token) return true;
}
return false;
};
const addToken = function(value) {
const token = makeString(value || '').trim();
if (token && !containsToken(token)) tokens.push(token);
};
// Support both array format (current) and legacy single-token format
if (getType(currentConfig.tokens) === 'array') {
for (let i = 0; i < currentConfig.tokens.length; i++) {
addToken(currentConfig.tokens[i]);
}
}
if (getType(currentConfig.token) === 'string') {
addToken(currentConfig.token);
}
addToken(verificationId);
// Persist merged token list; overwrite protection via `true` flag
if (!setInWindow(GLOBAL_NAMESPACE, { token: verificationId, tokens: tokens }, true)) {
data.gtmOnFailure();
return;
}
// Inject helper script; delegates success/failure callbacks
injectScript(HELPER_SCRIPT_URL, data.gtmOnSuccess, data.gtmOnFailure);
// Result on window after two tags fire with IDs 'abc123' and 'xyz789':
// window.yandexWebmasterVerification = { token: 'xyz789', tokens: ['abc123', 'xyz789'] }
```
--------------------------------
### GTM Sandboxed JS Test Scenarios for Yandex Webmaster Template
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
These tests cover various scenarios for the Yandex Webmaster GTM template, including successful first-time injection, token accumulation, deduplication, whitespace trimming, and failure paths.
```javascript
// Scenario: Preserves existing tokens and appends new verification ID
mock('queryPermission', function() { return true; });
mock('copyFromWindow', function() {
return { token: 'legacy456', tokens: ['abc123'] };
});
mock('injectScript', function(url, onSuccess) { onSuccess(); });
runCode({ verificationId: 'new789' });
// All three unique tokens are merged, oldest first
assertApi('setInWindow').wasCalledWith('yandexWebmasterVerification', {
token: 'new789',
tokens: ['abc123', 'legacy456', 'new789']
}, true);
assertApi('gtmOnSuccess').wasCalled();
assertApi('gtmOnFailure').wasNotCalled();
// ---
// Scenario: Does not duplicate an existing verification ID
mock('copyFromWindow', function() { return { tokens: ['abc123'] }; });
runCode({ verificationId: 'abc123' });
assertApi('setInWindow').wasCalledWith('yandexWebmasterVerification', {
token: 'abc123',
tokens: ['abc123'] // still only one entry
}, true);
// ---
// Scenario: Fails when verification ID is empty
runCode({ verificationId: '' });
assertApi('injectScript').wasNotCalled();
assertApi('gtmOnFailure').wasCalled();
```
--------------------------------
### GTM Template Parameter Definition
Source: https://context7.com/yandex/webmaster-gtm-template/llms.txt
Defines the 'verificationId' parameter for the GTM template. It requires a non-empty string value, which is the token from the Yandex Webmaster verification meta tag.
```json
// Template parameter definition (from template.tpl ___TEMPLATE_PARAMETERS___)
[
{
"type": "TEXT",
"name": "verificationId",
"displayName": "Webmaster verification ID",
"simpleValueType": true,
"help": "Paste the value from the content attribute of the Yandex Webmaster verification meta tag.",
"valueValidators": [
{ "type": "NON_EMPTY" }
]
}
]
```
--------------------------------
### Verify Meta Tag in Browser DevTools
Source: https://github.com/yandex/webmaster-gtm-template/blob/main/README.md
Use this JavaScript snippet in your browser's DevTools to check if the Yandex verification meta tags are correctly present in the document's head. It returns an array of the outerHTML of all found meta tags.
```javascript
Array.from(document.head.querySelectorAll('meta[name="yandex-verification"]')).map(function(meta) { return meta.outerHTML; })
```
--------------------------------
### Add Yandex Verification Meta Tag
Source: https://github.com/yandex/webmaster-gtm-template/blob/main/README.md
This HTML snippet represents the meta tag added to the head of the site. Ensure 'YOUR_ID' is replaced with your actual Yandex Webmaster verification ID.
```html
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.