Get Piwik Pro Audiences for segment data in Kameleoon

portrait de l'auteur Julie Trenque

Written by Julie Trenque

Updated on 02/13/2023

2 min

Intermediate

Manage your integrations

Analytics

CDP/DMP

CMP

CMS

Developers

Others

Was this content useful?

The available segments in your DMP are being made available by Piwik Pro Audiences via a REST-API.

On your Kameleoon account, click on “Administrate” in the left-hand menu, then on “Projects”.

Global custom script

On the card for your site or mobile app, click on “Configuration”: you’ll now access the configuration of your project on Kameleoon.

Unfold the “General” section.

The “Global custom script” insert will enable you to link Kameleoon to Matomo. Any JavaScript code you add in this insert will be executed every time the page is loaded. This feature is usually used to add complex tracking code or integration to other solutions.

Please copy and paste the code below into the Global script section.

const getPiwikProSegments = (visitorID) => {
        const clientID = 'Replace this with your client_id from generated credentials';
        const websiteID = 'Replace this with your project_id';
        const xhr = new XMLHttpRequest();
        xhr.onload = function run() {
            if (this.status === 200) {
                Kameleoon.API.Data.setCustomData(
                    'Piwik Pro segments',
                    JSON.parse(xhr.response)
                );
            }
        };
        xhr.open('GET', `https://customers.kameleoon.com/kameleoon/piwik/user/${visitorID}?id=${clientID}&website_id=${websiteID}`, true);
        xhr.send();
    };

    Kameleoon.API.Core.runWhenConditionTrue(() => typeof _paq !== 'undefined', () => {
        _paq.push([function init() {
            getPiwikProSegments(this.getVisitorId());
        }]);
    });

Click on “Validate” to apply the changes to your configuration.

Custom data

You then need to create a new custom data.

You will need:

  • your client_id & client_secret for generating Bearer Token (get Credentials);
  • your website_id.

Use the “Activation API Method” acquisition method. This code should be placed in the Advanced settings section of the custom data (don’t forget to add your client_id and website_id).

  let segmentPW = [];
     const clientID = 'Replace this with your client_id from generated credentials';
     const websiteID = 'Replace this with you website_id';
 

     const xhr = new XMLHttpRequest();
     xhr.onreadystatechange = function check() {
         if (this.readyState === 4) segmentPW = JSON.parse(xhr.response);
     };
     xhr.open('GET', `https://customers.kameleoon.com/kameleoon/piwik/segments/?id=${clientID}&website_id=${websiteID}`, false);
     xhr.send();
     return segmentPW; 

You will notice that in this code we call a Kameleoon Webservice which wraps the Piwik Pro API to get all the segments. This is needed to go throughout CORS policy issues.

The custom data should be set to the “list of” and “strings” types, the scope is “Page”.

External Segment Sync for Server-side

If you would like to use Matomo segment data with one of our SDKs, you will need to follow the guidelines provided in each SDK documentation and use the method retrievedatafromremotesource.

  • In this article :