Philipp Guttmann, LL. B.

Tutorial: Matomo (Piwik) Opt-Out without iFrame, just PHP

This tutorial explains how to create a simple and smart Matomo (Piwik) opt-out without the usage of iFrame, just with PHP

Nobody likes iFrames. So I decided to create a simple and smart opt-out for Matomo (prev. Piwik) without iFrame, only with PHP. This tutorial shows you how to create it. At the end of the article is the full code. Hope you like my solution so much as I do.

Introduction

At first you need a few variables, which are required to make our opt-out working:

$post = (isset($_POST['matomo_deactivated'])?$_POST['matomo_deactivated']:NULL); $cookie = (isset($_COOKIE["matomo_deactivated"])?$_COOKIE["matomo_deactivated"]:NULL); $dnt = (isset($_SERVER['HTTP_DNT'])?$_SERVER['HTTP_DNT']:NULL); $request_uri = $_SERVER['REQUEST_URI']; $domain = $_SERVER['HTTP_HOST'];

Our opt-out based on a form with posting methode, the detection of browser "Do Not Track" (DNT) option and the creation of a deactivation cookie when it is needed.

Button or information text

Depending on whether DNT is activated, a deactivation cookie is available or nothing of them, we return different outputs:

$no_tracking = '<p><i>You have '.($dnt != 1?'created a deactivation cookie':'activated the "Do Not Track" option in your browser').'.</i> Your visit are not tracked by Matomo. The Matomo script are not run.</p>'; $optout_button = '<form action="'.$request_uri.'" method="post"><button type="submit" name="matomo_deactivated" value="1" title="Cancel tracking your visit (Deactivation cookie will be created)">Cancel tracking your visit</button></form>';

Only if DNT is not activated and there is no deactivation cookie, we return our opt-out button. If you enter the button, the deactivation cookie will be created.

if ($dnt != 1 AND $post AND !$cookie) { setcookie('matomo_deactivated', $post, time() + (5 * 365 * 24 * 60 * 60), '/', $domain); $matomo_optout = $no_tracking; } elseif ($dnt != 1 AND !$cookie) { $matomo_optout = $optout_button; } else { $matomo_optout = $no_tracking; }

Deactivation of the Matomo script

If DNT is activated or a deactivation cookie exists, we have no longer need for the Matomo script in the HTML-head. So we deactivate it. Otherwise we return the script:

if ($cookie OR $dnt == 1 OR $post) { define('MATOMO_SCRIPT',false); } else { define('MATOMO_SCRIPT','<script>var _paq = window._paq || []; _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(["trackPageView"]); _paq.push(["enableLinkTracking"]); (function() {var u="https://matomo.'.$domain.'/"; _paq.push(["setTrackerUrl", u+"matomo.php"]); _paq.push(["setSiteId", 1]); var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript"; g.async=true; g.defer=true; g.src=u+"matomo.js"; s.parentNode.insertBefore(g,s);})();</script>'); }

Please note: I have the option disableCookies of Matomo activated. But of course that is not necessary. And do not forget to change URL and Site ID to yours.

Output in HTML

At last as an example the HTML-code of our opt-out page could be like the following:

echo '<!doctype html> <html> <head> <meta charset="utf-8" />'.(MATOMO_SCRIPT?' '.MATOMO_SCRIPT:NULL).' </head> <main> <header><h1>Matomo Opt-Out</h1></header> <div>'.$matomo_optout.'</div> </main> </html>';

Full code

$post = (isset($_POST['matomo_deactivated'])?$_POST['matomo_deactivated']:NULL); $cookie = (isset($_COOKIE["matomo_deactivated"])?$_COOKIE["matomo_deactivated"]:NULL); $dnt = (isset($_SERVER['HTTP_DNT'])?$_SERVER['HTTP_DNT']:NULL); $request_uri = $_SERVER['REQUEST_URI']; $domain = $_SERVER['HTTP_HOST']; $site_id = 1; $no_tracking = '<p><i>You have '.($dnt != 1?'created a deactivation cookie':'activated the "Do Not Track" option in your browser').'.</i> Your visit are not tracked by Matomo. The Matomo script are not run.</p>'; $optout_button = '<form action="'.$request_uri.'" method="post"><button type="submit" name="matomo_deactivated" value="1" title="Cancel tracking your visit (Deactivation cookie will be created)">Cancel tracking your visit</button></form>'; if ($dnt != 1 AND $post AND !$cookie) { setcookie('matomo_deactivated', $post, time() + (5 * 365 * 24 * 60 * 60), '/', $domain); $matomo_optout = $no_tracking; } elseif ($dnt != 1 AND !$cookie) { $matomo_optout = $optout_button; } else { $matomo_optout = $no_tracking; } if ($cookie OR $dnt == 1 OR $post) { define('MATOMO_SCRIPT',false); } else { define('MATOMO_SCRIPT','<script>var _paq = window._paq || []; _paq.push(["setDoNotTrack", true]); _paq.push(["disableCookies"]); _paq.push(["trackPageView"]); _paq.push(["enableLinkTracking"]); (function() {var u="https://matomo.'.$domain.'/"; _paq.push(["setTrackerUrl", u+"matomo.php"]); _paq.push(["setSiteId", '.$site_id.']); var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript"; g.async=true; g.defer=true; g.src=u+"matomo.js"; s.parentNode.insertBefore(g,s);})();</script>'); } echo '<!doctype html> <html> <head> <meta charset="utf-8" />'.(MATOMO_SCRIPT?' '.MATOMO_SCRIPT:NULL).' </head> <main> <header><h1>Matomo Opt-Out</h1></header> <div>'.$matomo_optout.'</div> </main> </html>';

Questions?

If you have further questions, please write me an e-mail at ed.nnamttug-ppilihp@liam and I will try to help :)