{"id":156,"date":"2020-06-25T18:00:00","date_gmt":"2020-06-25T18:00:00","guid":{"rendered":"http:\/\/techuserspace.com\/?p=156"},"modified":"2022-12-23T15:55:36","modified_gmt":"2022-12-23T23:55:36","slug":"wp-simple-google-analytics-code-installation","status":"publish","type":"post","link":"https:\/\/www.techuserspace.com\/wp-simple-google-analytics-code-installation\/","title":{"rendered":"WP Simple Google Analytics code Installation"},"content":{"rendered":"\n<p>So I needed to place my <strong>Google Analytics<\/strong> code in my <strong>WP<\/strong> 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.. <\/p>\n\n\n\n<p>So let&#8217;s make a little plugin, I have name it &#8220;<strong>wp simple google analytics<\/strong>&#8221; so create a folder named &#8220;<strong>wp simple google analytics<\/strong>&#8221; and a PHP file with same name.<\/p>\n\n\n\n<p> At this point I don&#8217;t know if such name exists in the WP archive of plugin but that&#8217;s the name I used..  the line <strong>17<\/strong> function <code data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">)<\/code><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>function_exists(&#39;wp_sinple_google_analytics&#39;);<\/code><\/pre><\/div>\n\n\n\n<p>checks to make sure we don&#8217;t have another plugin with the same name, which is a proper way to check for &#8220;<strong>name collision<\/strong>&#8220;.<\/p>\n\n\n\n<p>Then after making sure there isn&#8217;t a name collision in our little plugin we continue defining our previous named function above in the &#8220;<strong>function_exits()<\/strong>&#8221;  once defined then we place our code from google analytics after line <strong>26<\/strong> and before line <strong>30<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>&lt;?php\n\/*\n * Plugin Name: WP simple google analytics\n * Plugin URI: http:\/\/www.techuserspace.com\n * Description: A simple google analytics tracking insertion.\n * Author: tehcuserspace\/Neumann Valle\n * Author URI: http:\/\/www.techuserspace.com\n * Version: 0.0.9\n *\/\n?&gt;\n&lt;?php\n\nif (!defined(&quot;ABSPATH&quot;)) {\n    exit(); \/\/ Exit if accessed directly\n}\n\/\/ we make sure there isn&#39;t a plugin with same name\nif (!function_exists(&#39;wp_sinple_google_analytics&#39;)) {\n\n    \/**\n     * load google analytics codes is this\n     *\/\n    function wp_sinple_google_analytics()\n    {\n?&gt;\n\n        &lt;!-- here add your analytics code --&gt;\n\n\n\n        &lt;!--  end of google analytics --&gt;\n\n&lt;?php\n    }\n\n    \/\/ finally added the code to the footer\n    \/\/ if u would instead want it in the head\n    \/\/ then change wp_footer for wp_head\n    \/\/ as of this writting google asked the\n    \/\/ code be inserted in the head\n    add_action(&#39;wp_footer&#39;, &#39;wp_sinple_google_analytics&#39;);\n} else {\n\n    throw new Exception(&#39;another plugin with same name as the one you currently trying to activate has been activated..&#39;);\n}<\/code><\/pre><\/div>\n\n\n\n<p>The line <strong>40<\/strong><\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>add_action(&#39;wp_footer&#39;, &#39;wp_sinple_google_analytics&#39;);<\/code><\/pre><\/div>\n\n\n\n<p>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 <strong>head<\/strong> tag of your website or it will give you complains about not  being property placed. Changing &#8220;<strong>wp_footer<\/strong>&#8221; to &#8220;<strong>wp_head<\/strong>&#8221; would fix this, but as of the writing and after doing what webmaster tools requested, I used the &#8220;<strong>wp_footer<\/strong>&#8221; just to track users coming to the site.<\/p>\n\n\n\n<p>That\u2019s it, remember I am just a Tech User just like you. bye for now!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n<p><a href=\"https:\/\/www.techuserspace.com\/wp-simple-google-analytics-code-installation\/\" class=\"more-link\">Read more<span class=\"screen-reader-text\"> of WP Simple Google Analytics code Installation<\/span><span aria-hidden=\"true\"> &rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,13,2,11],"tags":[],"class_list":["post-156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-buddypress","category-php","category-programming","category-wordpress"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/posts\/156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/comments?post=156"}],"version-history":[{"count":40,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/posts\/156\/revisions"}],"predecessor-version":[{"id":2260,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/posts\/156\/revisions\/2260"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/media?parent=156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/categories?post=156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techuserspace.com\/wp-json\/wp\/v2\/tags?post=156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}