Go Back

Audience Manager: Number of Visits attribute

October 23, 2018

Creating an audience based on the number of visits may be an interesting use-case. It can come in handy whenever you wish to target a group of loyal visitors or serve them a specific page content based on their number of visits

To build this type of an audience in the Audience Manager we’ll need to create a custom metric for the numbers of visits. Such a metric will allow us to spot and group visitors who entered our website X number of times. We’ll show you how to do that with a simple method described below.

Please note that the following method uses a js-cookie library to process the cookies so be sure that you have it on your website

1. Create a Custom Asynchronous HTML tag

Open Piwik PRO Tag Manager and create a new Asynchronous tag of a Custom HTML tag type.
Paste this code into the code editor:

if (typeof Cookies.get(‘VisitNumber’) === ‘undefined’){
Cookies.set(‘VisitNumber’, 1, { expires: 30});
else {
var Vnumber = Number(Cookies.get(‘VisitNumber’))+1;
Cookies.set(“VisitNumber”, Vnumber);

This code adds a js-cookie, a small library helping with cookies in JavaScript. However, if you don’t want to implement this library feel free to write the code in plain JavaScript.

If a cookie doesn’t exist, in the users browser, the code will create a cookie. And if the cookie does exist the code will increment it’s value by one. The value collected by a cookie will show the number of visits of a user.

2. Add a trigger:

Create a trigger which will fire the tag only once per visit during the first pageview of a visitor. This way it will only fire once per visit thus providing correct number of visits of each visitors.

3. Create a Variable:

Now we need to create a Variable that will read the value from the cookie we’ve just created.
Open Variables in Tag Manager and create a new variable with the “Cookie” type

4. Send it as a Custom Event to Piwik:

Now go back to Tags section of your Piwik PRO Tag Manager and create another Custom Asynchronous HTML tag

Paste the following code into the code editor:

  If ( {{ VisitNumber }} > 2 ){
    _paq.push(['trackEvent', 'At least third visit', {{ VisitNumber }}]);

Once the visitor visits your website at least three times, this tag will send the event to Piwik. We’ll m
ap this event to an Audience Manager attribute later on, to make it available in Audience Manager.

For this event add the same trigger we’ve created before. Just make sure that you’ll set the tag firing priority to a lower as the previous one. To do that click the order icon in the top-right corner of the tags list and select “order by priority”. Then drag and drop the tags in the correct order as shown on the screenshot below:

5. Map the Custom Event as an attribute in Audience Manager

As the last step go into Audience Manager and choose “Attributes” from the top menu. Next click the “Create New Attribute” button
Name the attribute and click New Event Mapping

Map the attributes as shown in the screenshot below:

Hit Save and you’re all done. Now, you have an attribute which will be attached to every visitor that have visited your website at least three times. This attribute will be a pillar on which you will build an audience of frequent visitors.