Skip to content
Skip to main content
European Data Processing
← All platform guides

Install on PHP

A server-rendered snippet counts every page your application serves, and fills in the URL and referrer for visitors without JavaScript.

Before you paste anything

Every snippet below shows YOUR-SITE-ID . Replace it with your own site id, which is on the Tracking Code page of the site in your account. If you do not have an account yet, creating one and adding your domain takes about a minute.

Create a free account

Include the snippet in your layout

  1. Save the code below as a partial, for example templates/veritametrics.php.
  2. Include it from the layout every page renders, immediately before the closing </body> tag.
  3. Load any page your application serves.
templates/footer.php
<?php
// VeritaMetrics Analytics Tracking
// Place this code in your main template file before the closing </body> tag.
$verita_site_id = 'YOUR-SITE-ID';
$verita_domain = 'www.veritametrics.com';
$verita_current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$verita_referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
?>
<script>
(function() {
  if (typeof window === 'undefined') return;
  var siteId = '<?php echo htmlspecialchars($verita_site_id, ENT_QUOTES, 'UTF-8');?>';
  var trackingDomain = '<?php echo htmlspecialchars($verita_domain, ENT_QUOTES, 'UTF-8');?>';

  // 1. Initialize the Async Command Queue
  window.verita = window.verita || function() { (window.verita.q = window.verita.q || []).push(arguments) };
  window.verita.l = new Date().getTime();

  // 2. Load the enhanced tracker script
  var script = document.createElement('script');
  script.src = 'https://' + trackingDomain + '/api/tracker.js?siteId=' + siteId;
  script.async = true; script.defer = true;
  script.dataset.siteId = siteId;
  script.dataset.trackingDomain = trackingDomain;
  document.head.appendChild(script);

  // 3. Send the initial pageview using the queue
  window.verita('trackPageView');
})();
</script>
<noscript>
  <img src="https://<?php echo htmlspecialchars($verita_domain, ENT_QUOTES, 'UTF-8');?>/api/track-pageview?siteId=<?php echo htmlspecialchars($verita_site_id, ENT_QUOTES, 'UTF-8');?>&url=<?php echo urlencode($verita_current_url);?>&ref=<?php echo urlencode($verita_referrer);?>" alt="" width="1" height="1" style="display:none" aria-hidden="true" />
</noscript>

Note: The snippet reads $_SERVER for the current URL and the referrer, so it works behind any router.

Check that the data is arriving

  1. Open your site in a browser and load any page.
  2. In VeritaMetrics, open the site and click Tracking Code in the sidebar. The status turns active and your visit appears in the recent-pageviews list within a few seconds.

If nothing arrives, open your browser devtools on the Network tab and reload. You should see a request to /api/tracker.js that returns 200, then a request to /api/v1/c when you move or scroll.

Output buffering and early exits skip the footer

A redirect, a die() in an error branch, or a template that returns before the footer never renders the snippet, so a subset of pages goes uncounted and the gap follows your error paths rather than your traffic. Include the partial from the layout rather than from individual controllers.

Guides for other platforms

The same install, on a different stack.

Explore the documentation

Jump to another guide.