How to add Google Analytics in WordPress

Wordpress How to add Google Analytics in WordPress

In this article I will show you how to add the Google Analytics personal tracking code on your WordPress without a plugin. All you have to do is paste the following php code in your theme’s functions.php or in your custom plugin. Remember to change the XXXXXXXX code with your own tracking code provided by analytics.

  1. Log into your WordPress Admin interface.
  2. Go to Appearance.
  3. Go to Theme Editor.
  4. In the right menu find functions.php
  5. Copy and paste the code below at the end of functions.php
  6. Publish.

/** *Google Analytics on wp_head header*/
function ns_google_analytics() { ?>
  <script async src=""></script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'UA-XXXXXXXX-1');
add_action( 'wp_head', 'ns_google_analytics', 10 );

This post was updated on Friday / August 2nd, 2019 at 3:45 PM