Yandex Metrica is a powerful web analytics tool that helps website owners understand visitor behavior. The system tracks traffic sources, time spent on the site, popular pages, and user actions. A key feature of the service is the free provision of advanced tools such as Webvisor, click maps, and scroll maps.
For WordPress website owners, integrating analytics becomes a top priority after launching a project. Proper installation of the counter ensures data accuracy and provides access to valuable audience insights. In this guide, we will explore all methods of integrating Yandex Metrica with WordPress and choose the optimal option.
Why You Need Yandex Metrica
Web analytics allows you to make informed decisions when developing your site. You can see which pages attract the most visitors, where the audience comes from, and at what stage users leave the site. This data helps optimize content, improve usability, and increase conversions.
Yandex Metrica competes with Google Analytics and other analytics systems. Each platform has its own advantages and specific use cases. The main advantage of Metrica is free access to tools that are often paid add-ons in other systems. Webvisor records visitor actions, allowing you to see exactly how users interact with your site.
Click maps show where visitors click most often, while scroll maps demonstrate how far users scroll down the pages. These tools are especially useful for analyzing site usability and identifying problematic areas in the interface. Even if you do not plan an active advertising campaign, basic analytics will help you understand how interesting your site is to visitors.
In addition to behavioral metrics, Yandex Metrica provides detailed information about traffic sources: which search engines users come from, what queries they use, whether they come from social networks or other sites. This allows you to evaluate the effectiveness of your promotion and marketing activities. Analyzing demographic data such as age, gender, and visitor geography helps you better understand your target audience and adapt content to their needs.
| Feature | Yandex Metrica | Google Analytics |
|---|---|---|
| Webvisor (action recording) | Free | Paid (via add-ons) |
| Click maps | Free | Limited |
| Scroll maps | Free | No |
| E-commerce | Yes | Yes |
| Integration with advertising platforms | Yandex Direct | Google Ads |
Creating a Counter in Yandex Metrica
First, you need to register a counter for your site in the system. The process takes a few minutes and does not require special knowledge. Go to metrika.yandex.ru and log in to your Yandex account. If you do not have an account, create one, as it is free. After logging in, click the "Add counter" button.
In the form that opens, specify the counter name (any convenient name), the site address with the http or https protocol, and select your region's time zone. Enable the "Webvisor, scroll map, form analytics" option. This will activate advanced behavior analysis tools. If necessary, configure additional parameters: specify the site address with or without www, and add alternative domains if the site is accessible via multiple addresses.
After creating the counter, the system will generate a unique JavaScript code. This code needs to be placed on all pages of the site. Copy it completely, as you will need it in the next step. Note that the code contains a unique counter number, which is assigned automatically. Do not change the code manually, as this may lead to analytics errors.
Methods of Installing the Code on WordPress
There are several methods for adding the counter code to WordPress. Each method has its own advantages and disadvantages, which are worth considering in detail. The choice of method depends on your technical experience, project requirements, and preferences in site management.
Method 1: Editing Theme Files
The traditional method involves manually inserting the code into the active theme files. Usually, the code is placed in the header.php file before the closing </head> tag or in the footer.php file before the closing </body> tag. To do this, go to the WordPress admin panel, select the "Appearance" section, and click "Theme Editor". Find the required file and insert the counter code in the appropriate place.
Placing it in header.php ensures earlier loading of the counter and the start of tracking, while placing it in footer.php can slightly speed up the visual loading of the page for the user. The choice depends on your priorities: data accuracy or minimal delay in content display.
The main drawback of this approach is the loss of changes when updating the theme. When theme developers release a new version, all your edits will be replaced with the original files. This means you will need to reinstall the code after every update.
An additional risk is associated with the possibility of syntax errors. Accidentally deleting or changing an important part of the code can lead to site malfunction. For these reasons, this method is not recommended for use on production sites, especially if you do not have sufficient experience working with WordPress.
Method 2: Adding via functions.php
The functions.php file represents a more professional approach to WordPress customization. This file allows you to add your own code without modifying the main theme files. To install the counter, open the functions.php file of your theme and add a function that outputs the Metrica code. Use the wp_footer or wp_head hook depending on the desired placement.
Option A: Placement in the footer (wp_footer)
<?php
// Adding Yandex Metrica to the site footer
function add_yandex_metrika_footer() {
?>
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
(function(m,e,t,r,i,k,a){
m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)
})(window, document,'script','https://mc.yandex.ru/metrika/tag.js', 'ym');
ym(XXXXXXXX, 'init', {webvisor:true, clickmap:true, ecommerce:"dataLayer", referrer: document.referrer, url: location.href, accurateTrackBounce:true, trackLinks:true});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/XXXXXXXX" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
<?php
}
add_action('wp_footer', 'add_yandex_metrika_footer');
Option B: Placement in the header (wp_head)
<?php
// Adding Yandex Metrica to the site header
function add_yandex_metrika_head() {
?>
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
(function(m,e,t,r,i,k,a){
m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)
})(window, document,'script','https://mc.yandex.ru/metrika/tag.js', 'ym');
ym(XXXXXXXX, 'init', {webvisor:true, clickmap:true, ecommerce:"dataLayer", referrer: document.referrer, url: location.href, accurateTrackBounce:true, trackLinks:true});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/XXXXXXXX" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->
<?php
}
add_action('wp_head', 'add_yandex_metrika_head', 1);
If you want to place the code in the site header, replace wp_footer with wp_head in the last line. Replace 0 with your counter number and add the current code from Yandex Metrica.
The advantage of this method is that the code is preserved when updating the theme, provided you are using a child theme. However, working with functions.php requires basic knowledge of PHP and an understanding of the WordPress structure. An error in the code can cause critical site operation problems. Always back up your site before making changes to functions.php.
"Using functions.php to add analytics scripts is a common practice among WordPress developers. This method provides flexibility and control over how and when external scripts are loaded."
Method 3: Specialized Plugin
The safest and simplest method is to use a specialized plugin. Plugins automate the installation process, eliminate the risk of errors, and guarantee the preservation of settings during any updates. This method is ideal for site owners who do not want to deal with code or worry about potential issues when updating the theme or WordPress.
Specialized plugins solve several tasks at once: they protect your site from accidental errors, save time on installation and configuration, and ensure stable analytics operation without the need for constant monitoring. Unlike universal code insertion plugins that can slow down the site due to unnecessary features, specialized solutions work precisely and efficiently.
Our plugin for integrating Yandex Metrica is created specifically to solve this task. It is not overloaded with unnecessary features, works quickly, and is compatible with any themes and page builders. We have consciously abandoned complex settings and additional options, focusing on the main thing: safe and correct installation of the counter code. The plugin uses standard WordPress hooks, which guarantees no conflicts with other plugins and themes.
The Yandex Metrica plugin from IWS.BY safely integrates the counter via standard hooks. No conflicts when updating the theme and full compatibility with Elementor and Divi.
Regular updates ensure compatibility with new versions of WordPress and PHP. A simple interface allows you to install the counter in a couple of minutes without any technical knowledge. At the same time, you get full access to all Yandex Metrica features. The plugin does not make changes to the analytics operation; it only correctly places the official counter code on your site.
Comparison of Installation Methods
The choice of method depends on your experience and project requirements. Let's systematize the information to make an informed decision.
| Criterion | Theme Files | functions.php | IWS Plugin |
|---|---|---|---|
| Complexity | Medium | High | Low |
| Risk of errors | High | Medium | Minimal |
| Preservation during theme update | No | Only with a child theme | Yes |
| Required knowledge | HTML | PHP, WordPress | Not required |
| Installation time | 10-15 minutes | 15-20 minutes | 1-2 minutes |
| Compatibility with builders | Limited | Depends on implementation | Full |
| Need for backup | Mandatory | Mandatory | Not required |
As can be seen from the table, using a plugin saves time and reduces risks. This is especially important for site owners without technical education.
Step-by-Step Plugin Installation
The installation process takes literally a few minutes. Download the plugin from our website in the WordPress products section. The file is distributed for free.
In the WordPress admin panel, go to the "Plugins" section and select "Add New". Click the "Upload Plugin" button at the top of the page. Select the downloaded ZIP archive and click "Install Now", then "Activate".
After activation, a new "Yandex Metrica" item will appear in the menu, or you will find the settings in the "Settings" section. Open the plugin settings page. In the special field, paste the counter code you copied earlier from Yandex Metrica.
Choose the code placement location: in the site header or in the footer. Placement in the header ensures immediate tracking initialization, while placement in the footer can slightly speed up the initial page load. Click the "Save Changes" button. The plugin will automatically add the code to all pages of the site. No additional actions are required.
"Specialized plugins for inserting analytics use standard WordPress hooks, which guarantees correct operation with any themes and the absence of conflicts during updates."
Checking Functionality
After installation, be sure to verify that the counter is working correctly. Open the main page of the site in your browser. Return to the Yandex Metrica interface. A green indicator should appear next to the counter name, signaling successful code detection.
If the indicator does not light up immediately, wait 10-15 minutes. The system updates information with a slight delay. Check the "Visitors" report; you should see your visit.
For a detailed check, open the developer tools in your browser (F12 key), go to the "Network" tab, and refresh the page. Find the metric in the list of requests. There should be a request to Yandex servers with data transmission.
Questions and Answers
Conclusion
Installing Yandex Metrica on WordPress is a necessary procedure for any site that strives for development. Analytics provides data on the basis of which you can make informed decisions to improve content, design, and functionality.
Among the considered installation methods, using a specialized plugin is the safest and most effective. It eliminates the risk of errors, saves time, and guarantees the preservation of settings during any updates. Our plugin is created taking into account the best development practices. It is lightweight, fast, and does not create conflicts with other plugins or themes. Support for all analytics features and compatibility with page builders make it a universal solution for sites of any scale.