DNT.RSVP Last updated: 2021-07-05

Project Documentation

DNT.RSVP provides a simple way to respond to Do Not Track (DNT) requests with PHP.

Usage

DNT.RSVP PHP variant, responding to the DNT HTTP header server-side. Two methods for adding Do Not Track support with PHP are described below. The best method will depend heavily on your websites structure.

Include Method

Copy the dnt.php file from the DNT.RSVP GitLab repository into your website's PHP includes folder.

Open the dnt.php file in your editor. Modify the class dntphp variable $analytics, replacing the <script> on line 15 with your own analytics script.

public $analytics = <<<ANSC
<script>/* Your Analytics Script */</script>
ANSC;

Use the following PHP include snippet in your existing global footer/scripts/etc include. Alternatively, manually add the snippet before the closing </body> tag to all pages where analytics are desired.

Be sure to change the filepath /includes/dnt.php to the location the dnt.php file was copied to.

<?php include_once ($_SERVER['DOCUMENT_ROOT']."/includes/dnt.php");?>

Integrate Method

Copy class dntphp to your sites existing functions/classes/etc PHP file.

class dntphp {
	// dntphp.analytics
	// Define site analytics script
	public $analytics = <<<ANSC
	<script>/* Your Analytics Script */</script>
	ANSC;

	// dntphp.load()
	// Load analytics if DNT disabled
	public function load() {
		// Check if DNT is enabled
		if ($this->state() === true) {
			// DNT is true - don't load analytics
			echo "<!-- Do Not Track Enabled -->";
		} else {
			// DNT is false - load analytics
			echo $this->analytics;
		}
	}

	// dntphp.state()
	// Check DNT state, return true if enabled
	public function state() {
		// Verify DNT header exists and is true
		if (isset($_SERVER['HTTP_DNT']) && (int)$_SERVER['HTTP_DNT'] === 1) {
			// Return true
			return true;
		}
		// Return false otherwise
		return false;
	}
}

Modify the class dntphp variable $analytics, replacing the <script> on line 5 with your own analytics script.

public $analytics = <<<ANSC
<script>/* Your Analytics Script */</script>
ANSC;

Use the following PHP snippet in the file/include where you want to load your analytics. Make sure the file where you added the class to has been included.

<?php
	$dntphp = new dntphp;
	$dntphp->load();
?>

Verify

Verifying that DNT functionality is working is easy-peasy.

  • Make code changes live and clear any page caches.
  • Open the website in your browser of choice.
  • View the page source utilizing your browser tools - ctrl + u on Firefox/Chrome.
  • Locate where the analytics include would be in your page structure and notice either: analytics script present OR the comment <!-- Do Not Track Enabled -->.

You may not see the analytics script or the comment. In this case, it is most likely that the script performed as expected, but the comment was stripped from the page during HTML minification by either your web or proxy server(s).

Demo

A demonstration page is available for you to preview DNT.RSVP in action. WhateverBits also utilizes DNT.RSVP on this site to respect user choice in analytics gathering.

License

Project

DNT.RSVP is distributed on GitLab under The Unlicense.

License: https://gitlab.com/whateverbits/dnt.rsvp/-/blob/main/LICENSE

The Unlicense: https://unlicense.org/

The DNT.RSVP logo is a derivative of "ghost-solid" by Font Awesome, used under CC-BY-4.0.

CC-BY-4.0: https://creativecommons.org/licenses/by/4.0/

Font Awesome License: https://fontawesome.com/license/free

Font Awesome Source: https://fontawesome.com/v5.15/icons/ghost