Content
- page: Overview of basic terms and preparation for Matomo hosting
- page: Download and install Matomo
- page: Placing tracking code on WordPress pages
- page: Placing tracking code on Drupal pages
- page: Placing and customizing tracking code on custom pages
- page: Make important settings after installation
- page: Installation of GeoIP 2 (PHP) based geolocation system
- page: Matomo migration of a complete system to PHP-FPM based server API operation
The 5. page content
Continuation
The previous page is of Drupal type CMS we implemented the Matomo (Piwik) tracking code, and now we'll look at how to paste JavaScript into custom web pages.
Generate tracking code
In this chapter, we'll step-by-step create a more sophisticated tracking code to provide you with comprehensive and accurate statistics about your website traffic. You can also go through this section if you are using a content management system, because the Matomo plugin in it does not know all the settings, but it allows you to place custom codes where you can place the settings you know here.
The basic code
First, get the source code, which you can do on the Matomo settings page. To do this, click the gear in the top right corner and then, under the left-hand menu, go to the "Tracking Code" menu item.
Scroll down a JavaScript Tracking Part One:
Here are some options that, if enabled, will include the appropriate lines in the tracking code instance below:
Here, for us, the key code is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!-- Matomo --> <script type="text/javascript"> var _paq = window._paq || []; /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ _paq.push(['trackPageView']); _paq.push(['enableLinkTracking']); (function() { var u="//192.168.1.120/matomo/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '3']); var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); })(); </script> <!-- End Matomo Code -->
Of this, what we need to set individually is in lines 8 and 10: in line 8 we can enter the path to our own Matomo instance, and in line 10 we can enter the ID of the website we want to measure. All other parts of this code are already generic, so the same for each code instance (or there will be settings that we also need to specify individually). With this code, a normal-use website traffic measurement already works, but there are still many configuration options - some of which we've learned about before through extensions to various CMS systems - the most important of which we'll review here.
Matomo settings
First, let's go over the Matomo interface-enabled tracking code settings, only from the perspective of JavaScript codes.
Tracking subdomains
If you want your visitors to be tracked across all subdomains (if there are more) then insert this line of code 5. line trackPageview sor edge:
_paq.push(["setCookieDomain", "*.domainnev.tld"]);
Here, of course, set up the web page you want to measure domain name.
Add a domain name to the address
With this feature, Matomo will append the domain name before or after the statistics that appear in the statistics:
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
This is also the line of the trackPageView edge have to do.
Known domain names
A setDomains function to control which domains, subdomains, subdirectories, etc. belong to your site. So if a visit or event occurs on any of these, it will not count as an exit page in the statistics. Any visit other than these (such as an outbound link) is considered an exit page.
_paq.push(["setDomains", ["*.domain.tld"]]);
This is also the line of the trackPageView edge have to do.
Tracking without JavaScript
It is also possible to measure visits where JavaScript code is not running. This is a part can be solved:
<noscript><p><img src="//192.168.1.120/matomo/matomo.php?idsite=3&rec=1" style="border:0;" alt="" /></p></noscript>
This line should be placed at the end of the base code a after row. Also, replace the path to your own Matomo instance, as well as idsite = 3 parameter with the website ID in our own Matomo system - as we did in the source code.
Tracking unique variables
A setCustomVariable function can also be used to place unique variables in the code for more personalized measurements:
_paq.push(["setCustomVariable", 1, "valtozo", "ertek", "visit"]);
Where the meaning of the parameters is as follows:
- setCustomVariable: The function itself is what the JavaScript tracking code calls
- 1: Index / location of the variable. Here you can place variables up to 1-5. Always put the same variable on the same index. This allows us to place different variables in 5.
- value: The value given to the variable. Here, for example, we can pass values generated from PHP to the code.
- visit: And that is the changing power. If set to "visit", the variable will be valid for the entire visit session. For example, you can enter the user name, etc. And if you specify a "page" scope here, you can pass separate values to page downloads within a visit, which can vary from page to page. For example, information about the page you are viewing: Blog page title, author, or even the product code of a product in a web store, and so on.
The variables placed in this way are collected by the Matomo server during visits and can then be filtered, grouped, etc. in the statistics.
This too trackPageview in front of you.
Enable client-side DoNotTrack
We can also provide the ability to turn off tracking from the client side by inserting this line into the code trackPageview edge:
_paq.push(["setDoNotTrack", true]);
Disable tracking cookies
_paq.push(["disableCookies"]);
If we place this line in the tracking code (also in the trackPageview ), you will block all subsequent cookies. Existing cookies will be deleted the next time you load a page.
Track marketing campaigns
We may also follow our own marketing campaigns, such as newsletters and other paid advertisements. To do this, we need to enter the name of the campaign and a keyword. These are used to identify where the visitor came from for statistics. We need to place two lines in the tracking code a trackPageview edge:
_paq.push(["setCampaignNameKey", "kampanyneve"]);
_paq.push(["setCampaignKeywordKey", "kulcsszo"]);
Then we need to put the URL of our page in the links of the given campaign (eg newsletter) (example):
http(s)://weboldalam.hu/erkezesi_oldal.php?pk_campaign=kampanyneve&pk_kwd=kulcsszo
The Matomo server will then identify the campaign and associate it with the right place in the statistics. Of course, the landing page must have the source inside this tracking code supplement.
Events and Goals
Event tracking
On today’s modern websites, not every event will result in a full page load. For example, clicking on a JavaScript link won't load the page, so the tracking code won't run again until the entire page loads at the beginning. Therefore, we also have the ability to track these JavaScript click events separately, which also have different appearances in Matomo's statistical interfaces. All you have to do is load at least the basic tracking code at the top of the page and then create the JavaScript link like this:
<a href="#" onclick="_paq.push(['trackEvent', 'Kategória', 'Érték']);">Linkszöveg</a>
Where you can enter a unique identifier instead of Category, such as where the click occurred (Menu, product list, footer, etc.), and a unique identifier instead of Value, such as the product name or code for a display link on a web store product page , etc. The Matomo server will then collect these independent click events as well.
Follow Goal
In Matomo, goals can be used to differentiate the part of a visit where a visitor performs a specific action at a particular URL. These can be, for example, a newsletter subscription, registration, download, purchase, etc. on the site. Goals must be entered on the Matomo interface, where you must set the conditions for fulfilling the goal, such as the pattern in the URL and the activity at the URL that matches the condition (click on a link, download, spend X time on that page, click on external links, etc.). If these are met, the goal is created in Matomo's statistics. However, there are cases where these criteria cannot be used to achieve a goal, such as:
- When a user submits a form
- When a user watches a video
- When a user checks out of his cart
- etc
For such cases, similar to events, a separate activating part can be placed, which triggers the specified target.
Again, this requires the Matomo tracking code to be fully loaded when the page is fully loaded, and the activation portion to be placed where necessary. This can be done, for example, in an HTML form onsubmit event, or a link like the one above onclick part:
<form onsubmit="_paq.push(['trackGoal', 1]);">
...
</form>
vagy
<a onclick="_paq.push(['trackGoal', 1]);">Linkszöveg</>
Where the trackGoal function name must be followed by the pre-recorded destination ID.
Further settings
Accurate measurement
Between two page loads, you can easily calculate the time on the (first) page. However, we do not know when the last page was staying when the visitor left our site. The enableHeartBeatTimer option. The bottom line is that at specified intervals, the browser sends a "life signal" to the Matomo server, so we can get feedback before we leave the page. To do this, we need to add the following line:
_paq.push(['enableHeartBeatTimer', x]);
Where x is the number of seconds to send the life signal to the server.
Note that overly frequent cycles will overload your server, so set your resources to the right value.
Unlike the previous one, this option is now set to trackPageview sora after need to put in.
Internal search measurement
If the web site you want to measure has its own search engine, then Matomo (Piwik) web analytics system can easily track your searches. This can be configured as usual for page views so far trackPageview instead of we apply the track site search function on search pages as follows:
_paq.push(['trackSiteSearch', kulcsszó, kategória, találatok_száma]);
Where the parameters are:
- keyword: When you arrive at the results page, the search term must be passed to the function here.
- category: (optional) If there is a category system developed on the site, you can also enter the category here.
- Number of hits: (optional) Here you can specify how many results the search returned.
So again, while in the case of traditional page downloads we use the trackPageView function, in the search engine result pages we use the trackSiteSearch function. Accordingly, Matomo statistics will also display search events instead of page downloads, which will be marked with a different icon.
User ID Tracking
If the site has user management, we can also forward the unique identifier of logged in users to the Matomo server, which provides transparent statistics across multiple browsers and devices. To do this, place the following line in your tracking code:
_paq.push(['setUserId', 'felhasznalo_id']);
Where user_id should be replaced with the user ID registered by the page.
This is trackPageView edge have to do.
This can be easily accomplished by placing it in a conditional branch of a server-side script. If the user is logged in, we will put this line in the source code ...
These were the most important in my opinion, but there are still a number of setting options available read here more.
A next page we continue with the necessary settings after installing the Matomo (Piwik) system.
Navigation
- To post registration and login required
- 52 views