How Piwik PRO audiences fit into your workflow
- Retrieve the segments: Add the global custom script, so that each visitor’s Piwik PRO segments reach Kameleoon.
- Create the custom data: Create the Piwik PRO segments custom data, and add the list of segments to it.
- Build a segment: Target visitors in one or more Piwik PRO audiences.
- Use the segments in Feature Experimentation: Store each visitor’s segments in Kameleoon, and read them with an SDK.
Before you begin
- Piwik PRO account: an active Piwik PRO account with the audiences you want to use.
- Piwik PRO credentials: a
client_idandclient_secretfor generating a bearer token, as described in the Piwik PRO authentication reference. You also need yourwebsite_id, which Piwik PRO calls the site or app ID. To find it, read the Piwik PRO help article on site or app IDs. The scripts in this article use onlyclient_idandwebsite_id. - Piwik PRO tracker: Your site loads the Piwik PRO tracker, which provides the
_paqobject that the global custom script waits for. - Kameleoon tag: Your site loads the Kameleoon tag, which provides the
Kameleoon.APImethods the scripts call.
Retrieve Piwik PRO segments for each visitor
The global custom script runs on every page where the Kameleoon tag loads. It waits for the Piwik PRO tracker, and then it requests the visitor’s segments from a Kameleoon web service. The script requests segments only after the tracker loads. If the tracker never loads, the script sets no segments for the visitor.Add the global custom script
Add the script to your project’s global custom script, so that Kameleoon retrieves each visitor’s Piwik PRO segments.- Click Admin > Projects, then click Edit on your project’s row.
-
Click the Scripts tab, and then click Edit next to Global custom script.

-
Paste the following script into the Global custom script editor, and replace the
client_idandwebsite_idplaceholders with your values. - Click Save changes.
Create custom data for the Piwik PRO segments
The custom data has two parts. The global script fills it with each visitor’s segments, and a second script lists every segment as an option in the segment builder. Because the custom data uses the Page scope, Kameleoon resets its value on each new page, so the global script sets the segments again each time a page loads.Create the custom data
Create the custom data that the global script fills with each visitor’s segments.- Click Settings > Custom Data, and then click New custom data.
- Enter
Piwik PRO segmentsas the name, and select your project. The global script sets the custom data by this name, so enter it exactly. - Keep Retrieval method set to Kameleoon Activation API, which is the default.
- Set Type to List, Format to String, and Scope to Page. With the Page scope, Kameleoon resets the value for each new page, and the global script sets it again when the page loads.
- Click Next to open the options.
Add the segment list to the custom data
Add the script that returns the Piwik PRO segments as values for the custom data.- Turn on Save the values for the targeting condition associated with the custom data point. A code field appears.
-
Paste the following script into the code field, and replace the
client_idandwebsite_idplaceholders with your values. - Click Create.

Build a segment from Piwik PRO audiences
Build a segment that targets visitors who belong to one or more Piwik PRO audiences.- Click Settings > Segments, and then click New segment.
- In the Conditions column, open the Custom data tab, and then select Piwik PRO segments.
- Select the is among the values condition, and then select the audiences you want to target.
- Choose whether to include or exclude the visitors who match the condition.
Use Piwik PRO audiences in Feature Experimentation
The global custom script runs in the browser, so the segments it sets aren’t available to feature flags that Kameleoon’s SDKs evaluate. To use Piwik PRO audiences in a feature flag, store each visitor’s segments on Kameleoon’s servers, and then read them with an SDK.Store and read the segments with an SDK
Store each visitor’s segments in Kameleoon, and then set them as custom data in your SDK code.- Post each visitor’s segments to the Data API Map endpoint, using the visitor code or a user ID as the key. See the Map endpoint reference.
- Click Settings > Custom Data, and then click New custom data. Enter a name for the custom data, such as
Piwik PRO audiences, and select your project. - Set Retrieval method to Kameleoon SDK method, Type to List, and Format to String. Choose the Scope that fits your feature flag.
- In your SDK code, call
getRemoteData()with the visitor code or user ID, read the segments from the returned JSON, and set them withaddData().