WP Simple Google Analytics code Installation

WP Simple Google Analytics code Installation

So I needed to place my Google Analytics code in my WP website and I decided to code something quick that works and that I know my self is safe to use, because I believe having bunch of plugins from authors that really have good intentions but in the end are Humans like you and me can make an error and leave your site vulnerable..

So let’s make a little plugin, I have name it “wp simple google analytics” so create a folder named “wp simple google analytics” and a PHP file with same name.

At this point I don’t know if such name exists in the WP archive of plugin but that’s the name I used.. the line 17 function )

function_exists('wp_sinple_google_analytics');

checks to make sure we don’t have another plugin with the same name, which is a proper way to check for “name collision“.

Then after making sure there isn’t a name collision in our little plugin we continue defining our previous named function above in the “function_exits()” once defined then we place our code from google analytics after line 26 and before line 30

<?php
/*
 * Plugin Name: WP simple google analytics
 * Plugin URI: http://www.techuserspace.com
 * Description: A simple google analytics tracking insertion.
 * Author: tehcuserspace/Neumann Valle
 * Author URI: http://www.techuserspace.com
 * Version: 0.0.9
 */
?>
<?php

if (!defined("ABSPATH")) {
    exit(); // Exit if accessed directly
}
// we make sure there isn't a plugin with same name
if (!function_exists('wp_sinple_google_analytics')) {

    /**
     * load google analytics codes is this
     */
    function wp_sinple_google_analytics()
    {
?>

        <!-- here add your analytics code -->



        <!--  end of google analytics -->

<?php
    }

    // finally added the code to the footer
    // if u would instead want it in the head
    // then change wp_footer for wp_head
    // as of this writting google asked the
    // code be inserted in the head
    add_action('wp_footer', 'wp_sinple_google_analytics');
} else {

    throw new Exception('another plugin with same name as the one you currently trying to activate has been activated..');
}

The line 40

add_action('wp_footer', 'wp_sinple_google_analytics');

makes sure adds our Analytics code in the footer, bare in mind that Google asks to validate in Google Webmasters that your code be placed in the head tag of your website or it will give you complains about not being property placed. Changing “wp_footer” to “wp_head” would fix this, but as of the writing and after doing what webmaster tools requested, I used the “wp_footer” just to track users coming to the site.

That’s it, remember I am just a Tech User just like you. bye for now!