How to Include Google Analytics Tracking Code with PHP

How to Include Google Analytics Tracking Code with PHP

  1. Copy your tracking code from Google
  2. Create a new file in your document root called gtag.js (you can name this file anything you want, this is only an example)
  3. Paste your Google tracking script in the gtag.txt file
  4. Insert this code into your website’s index file (typically index.php:Copy<?php include_once “gtag.js”;?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-145188816-8"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-145188816-8');
</script>
Open chat