Let’s say you have multiple websites 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 (that they have the same Piwik PRO account). In this article, we’ll show you how this can be achieved.
Find your website’s ID and account’s URL
First, you will need your website’s ID and the URL of your account.
To find the ID for the website, follow these steps:
- Go to Menu > Administration.
- Navigate to Sites & apps.
- On the left, select the website for which you want to find an ID.
- The ID is under the name of your website.
The URL of your account is the address you use to access Piwik PRO, for example, https://example.piwik.pro/
.
Create a file
Now create a file where you will 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 example code 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();
}
Change the container code
Now we need to change a few things in the container code. Assuming your account is me.piwik.pro
and your website’s ID is 08053c93-2b69-4a5f-ad7f-551533300d21
, follow these steps:
Container code for asynchronous tags
- Replace
tags.src="//client1.piwikpro.test/containers/"+id+".js"
withtags.src="//container.php?type=async&uuid="+id
. - Replace
<iframe src="//me.piwik.pro/containers/08053c93-2b69-4a5f-ad7f-551533300d21/noscript.html"
with<iframe src="//container.php?type=async&uuid=08053c93-2b69-4a5f-ad7f-551533300d21"
Here’s an example with a modified code:
<script type="text/javascript">
(function(window, document, dataLayerName, id) {
window[dataLayerName]=window[dataLayerName]||[],window[dataLayerName].push({start:(new Date).getTime(),event:"stg.start"});var scripts=document.getElementsByTagName('script')[0],tags=document.createElement('script');
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")||document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?14:-1);
var qP=[];dataLayerName!=="dataLayer"&&qP.push("data_layer_name="+dataLayerName),isStgDebug&&qP.push("stg_debug");var qPString=qP.length>0?("?"+qP.join("&")):"";
tags.async=!0,tags.src="//container.php?type=async&uuid="+id+qPString,scripts.parentNode.insertBefore(tags,scripts);
!function(a,n,i){a[n]=a[n]||{};for(var c=0;c<i.length;c++)!function(i){a[n][i]=a[n][i]||{},a[n][i].api=a[n][i].api||function(){var a=[].slice.call(arguments,0);"string"==typeof a[0]&&window[dataLayerName].push({event:n+"."+i+":"+a[0],parameters:[].slice.call(arguments,1)})}}(i[c])}(window,"ppms",["tm","cm"]);
})(window, document, 'dataLayer', '8be0e1b9-7efd-463b-82c1-33a01ff1e3a1');
</script><noscript><iframe src="//container.php?type=async&uuid=08053c93-2b69-4a5f-ad7f-551533300d21" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
Container code for synchronous tags
- Replace
document.write('<script src="//me.piwik.pro/containers/'+id+'.sync.js' + qPString + '"></' + 'script>');
withdocument.write('<script src="//container.php?type=sync&uuid='+id+ qPString + '"></' + 'script>');
.
Here’s an example with a modified code:
<script type="text/javascript">
(function(window, document, dataLayerName, 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")||document.cookie.match("stg_debug"))&&!window.location.href.match("stg_disable_debug");stgCreateCookie("stg_debug",isStgDebug?1:"",isStgDebug?14:-1);
var qP=[];dataLayerName!=="dataLayer"&&qP.push("data_layer_name="+dataLayerName),isStgDebug&&qP.push("stg_debug");var qPString=qP.length>0?("?"+qP.join("&")):"";
document.write('<script src="//container.php?type=async&uuid='+id+ qPString + '"></' + 'script>');
})(window, document, 'dataLayer', '8be0e1b9-7efd-463b-82c1-33a01ff1e3a1');
</script>