WordPress

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.webcodegeeks.com/wp-content/litespeed/localres/aHR0cHM6Ly93d3cuZ29vZ2xldGFnbWFuYWdlci5jb20vZ3RhZy9qcw==?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.

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.
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
6 years ago

The process of adding the google analytics script in the WordPress can be very useful for those who are trying to do that and can get help from here. I have done the same to add the script but at the time of the adding My system got crashed and I got the Dell error code 2000-0142 in my system.

Back to top button