Home » WordPress » WordPress DIY: Adding Google Analytics Script to your Blog

About Prahlad Yeri

Prahlad Yeri
Prahlad is a freelance software developer working on web and mobile application development. He also likes to blog about programming and contribute to opensource.

WordPress DIY: Adding Google Analytics Script to your Blog

Adding a custom script element to your WordPress blog is really straightforward if you know what you are doing and there isn’t any need to install a third-party plugin for this.

Now, for something like spam protection (Akismet) or adding contact forms (Contact Form Seven), its quite understandable, but if you take the approach of adding third party plugins for every little plus and minus operation (such as adding a Google Analytics script), then its neither good for the maintenance nor security of your WordPress blog.

In this article, I’m going to explain how to create a simple custom plugin for your WordPress blog that you’ll hand-code yourself. The custom plugin will be quite generic and can be later used for adding other elements too such as maybe twitter card attributes, etc.

Create the following PHP Plugin file in the code editor of your choice and save it as /wp-content/plugins/custom_headers.php in your WordPress installation.

<?php 
/**
 * @package Custom Headers
 * @version 0.1.0
 */
/*
Plugin Name: Custom Headers
Plugin URI: http://github.com/prahladyeri/custom-headers/
Description: Add a bunch of custom headers before the head tag.
Author: Prahlad Yeri
Version: 0.1.0
Author URI: https://www.prahladyeri.com/
*/

function add_analytics_header() {
$analytics=<<<EOD
<-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-X">
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXX-X');
</script>
EOD;
	echo $analytics;
}
add_action('wp_head', 'add_analytics_header');

Just remember to update the package meta stuff (Plugin Name, Author, etc.) as it suits you and put your own Analytics ID in place of UA-XXXXXXX-X.

Its as simple as that, your plugin is ready! Just enable this plugin by going to Plugins->Installed Plugins in your WordPress admin panel.

Published on Web Code Geeks with permission by Prahlad Yeri, partner at our WCG program. See the original article here: WordPress DIY: Adding Google Analytics Script to your Blog

Opinions expressed by Web Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of