Go Back

Hide the presence of the Piwik PRO container from the website

June 6, 2018

Let’s say that you have multiple websites which all are tracked to the same Piwik PRO instance but you don’t want the outside world to know that these websites are connected to each other (same Piwik PRO account) – in this article, I will show you how this can be achieved.

On the very beginning, you will need your website’s ID and URL address of the Piwik PRO instance. Website ID you can find in the Installation section

In our example website ID is 08053c93-2b69-4a5f-ad7f-551533300d21
Piwik PRO instance URL address https://salesdemo.piwik.pro/

Now create a file to download the container code. This file has to be named container.php. In this file, you should change only the hostname which is located in the first line of the code example below. The rest remains the same.


<?php
 
$hostname = 'https://salesdemo.piwik.pro/';
 
$type = cleanInput($_GET['type']);
$websiteId = validateUuid(cleanInput($_GET['uuid']));
 
$url = sprintf('%s%s', $hostname, $websiteId);
 
switch ($type){
    case 'sync':
        $url = sprintf('%s%s', $url, '.sync.js');
        break;
    case 'async':
        $url = sprintf('%s%s', $url, '.js');
        break;
    case 'noscript':
        $url = sprintf('%s%s', $url, '/noscript.html');
        break;
}
if (isset($_GET['stg_debug'])) {
    $url = sprintf('%s%s', $url, '?stg_debug');
}
 
header('Content-Type:application/javascript');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_exec($ch);
 
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
    http_response_code(curl_getinfo($ch, CURLINFO_HTTP_CODE));
}
curl_close($ch);
 
function cleanInput($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
 
    return $data;
}
 
function validateUuid($uuid) {
    return preg_match('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', $uuid)? $uuid : die();
}

Now we need few changes in the snippet code.

First of all, you need to change the container source (in the Installation snippet) to “container.php”.

Secondly, you need to add a query string with script type (sync, async, noscript) and website ID (need to be valid UUID). Last important change is to change isStgDebug&&(tags.src=tags.src+”?stg_debug”) to isStgDebug&&(tags.src=tags.src+”&stg_debug”) in async code and (isStgDebug ? “?stg_debug” : “”) to (isStgDebug ? “&stg_debug” : “”) in sync code (so you need to change ?stg_debug to &stg_debug), without this change you will get incorrect URL when you try to run debugger. Below you’ll find prepared website code:

Website code for asynchronous tags:


<!-- Start Piwik PRO Tag Manager code --><script type="text/javascript">
(function(window, document, script, dataLayer, id) {
function stgCreateCookie(a,b,c){var d="";if(c){var e=new Date;e.setTime(e.getTime()+24*c*60*60*1e3),d="; expires="+e.toUTCString()}document.cookie=a+"="+b+d+"; path=/"}var isStgDebug=(window.location.href.match("stg_debug")||window.document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?365:-1);
window[dataLayer]=window[dataLayer]||[],window[dataLayer].push({start:(new Date).getTime(),event:"stg.start"});var scripts=document.getElementsByTagName(script)[0],tags=document.createElement(script),dl="dataLayer"!=dataLayer?"?dataLayer="+dataLayer:"";tags.async=!0,tags.src="container.php?type=async&uuid=b8cb12c5-97b8-41ac-8fbc-016425972d27"+dl,isStgDebug&&(tags.src=tags.src+"&stg_debug"),scripts.parentNode.insertBefore(tags,scripts);
})(window, document, 'script', 'dataLayer', 'b8cb12c5-97b8-41ac-8fbc-016425972d27');
</script><noscript><iframe src="container.php?type=noscript&uuid=b8cb12c5-97b8-41ac-8fbc-016425972d27" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript><!-- End Piwik PRO Tag Manager code -->

Website code for synchronous tags:


<<!-- Start Piwik PRO Tag Manager code --><script type="text/javascript">
function stgCreateCookie(a,b,c){var d="";if(c){var e=new Date;e.setTime(e.getTime()+24*c*60*60*1e3),d="; expires="+e.toUTCString()}document.cookie=a+"="+b+d+"; path=/"}var isStgDebug=(window.location.href.match("stg_debug")||window.document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?365:-1);
var url = "container.php?type=sync&uuid=08053c93-2b69-4a5f-ad7f-551533300d21" + (isStgDebug ? "&stg_debug" : "");
document.write('<script src="' + url + '"></' + 'script>');
</script><!-- End Piwik PRO Tag Manager code -->