{"id":145497,"date":"2015-09-05T11:00:44","date_gmt":"2015-09-05T15:00:44","guid":{"rendered":"http:\/\/premium.wpmudev.org\/blog\/?p=145497"},"modified":"2019-10-15T05:24:29","modified_gmt":"2019-10-15T05:24:29","slug":"create-google-analytics-plugin","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/create-google-analytics-plugin\/","title":{"rendered":"How To Create a Google Analytics WordPress Plugin To Start Using Today"},"content":{"rendered":"<p>Want to create a plugin for Google Analytics tracking in WordPress? Here&#8217;s how to get your plugin created today!<\/p>\n<p>Adding Google Analytics to your WordPress site will help you track who&#8217;s been visiting your site, what they&#8217;ve been looking at, and how they got there.<\/p>\n<p>The simplest and easiest way to do this is to install a Google Analytics WordPress plugin, such as\u00a0<a href=\"https:\/\/wpmudev.com\/project\/beehive-analytics-pro\/\" target=\"_blank\">Beehive<\/a>, which lets you quickly access a post, page, site or network performance overview without leaving your WordPress or Multisite dashboard.<\/p>\n<p>But sometimes you might want to keep things a bit simpler and insert the analytics tracking code into your site yourself. And other times you might want to roll up your sleeves, play with code, and create analytics plugins of your own.<\/p>\n<p>Google tells you to insert the code in the <code>&lt;head&gt;<\/code>\u00a0section of every page on your site, but with WordPress you don&#8217;t need to do this. You can create a simple plugin that will automatically insert it into every new page that&#8217;s created on your site.<\/p>\n<p>The good thing about adding Google Analytics to WordPress using a plugin is that if you update your theme, it will not remove the analytics code from your site.<\/p>\n<p>In this tutorial, we&#8217;ll show you\u00a0how to create a simple Google Analytics plugin that will add the code to every page of your WordPress site.<\/p>\n<h3>What You&#8217;ll Need<\/h3>\n<p>To create a Google Analytics plugin for WordPress, you&#8217;ll need just a few things:<\/p>\n<ul>\n<li>A live WordPress installation running the theme of your choice.<\/li>\n<li>Access to your site&#8217;s files via FTP.<\/li>\n<li>A code editor or text editor.<\/li>\n<\/ul>\n<p>I&#8217;m using <a href=\"https:\/\/panic.com\/coda\/\" target=\"_blank\">Coda<\/a>, which gives me code editing functionality and FTP all in one place, but you could use any code or text editor along with a free FTP client such as <a href=\"https:\/\/filezilla-project.org\" target=\"_blank\">Filezilla<\/a>.<\/p>\n<p><em>Note: you can find the source code for this post on <a href=\"https:\/\/github.com\/wptraining\/tutorials\/blob\/master\/wpmudev-google-analytics\" rel=\"noopener\" target=\"_blank\">Github<\/a>, so if you&#8217;re having any problems, check that out.<\/em><\/p>\n<h3>The Process<\/h3>\n<p>If you haven&#8217;t created a plugin before, don&#8217;t worry. It&#8217;s much simpler than you might think. To create this plugin, you&#8217;ll follow five\u00a0steps:<\/p>\n<ol>\n<li>Check your active theme&#8217;s <code>header.php<\/code> file to ensure it includes the <code>wp_head<\/code> hook.<\/li>\n<li>Create a new file in your plugins folder.<\/li>\n<li>Get your analytics tracking code from the Google Analytics site.<\/li>\n<li>Add some code\u00a0including the tracking code to your plugin.<\/li>\n<li>Activate the plugin.<\/li>\n<\/ol>\n<p>So let&#8217;s get started!<\/p>\n<h3>Checking Your Theme File for the wp_head Hook<\/h3>\n<p>Plugins insert code into your site by attaching code to a hook that is already written into your theme. Any well-written theme will have the <code>wp_head<\/code> hook in its <code>header.php<\/code> file. This hook is used by a multitude of plugins to inserts scripts, styles and more.<\/p>\n<p>If you&#8217;ve downloaded your theme from the WordPress theme repository then it should have the <code>wp_head<\/code> hook in the right place, but let&#8217;s check just in case.<\/p>\n<p>In your FTP program, access the <code>wp-content<\/code> folder in your WordPress installation. Inside that, you&#8217;ll find a themes folder and then a folder for your active theme. For example, the folder containing the theme files for the Twenty Nineteen theme would be at <code>wp-content\/themes\/twentynineteen<\/code>.<\/p>\n<p><em>Note: It&#8217;s only the active theme you need to be concerned with. Ignore any other themes you have installed on your site.<\/em><\/p>\n<p>In your theme folder, you&#8217;ll find a file called <code>header.php<\/code>. Open it.<\/p>\n<p>The <code>header.php<\/code> file includes all of the code at the beginning of each page, including the <code>&lt;head&gt;<\/code> section of the page. You should find the <code>wp_head<\/code> hook at the end of the <code>&lt;head&gt;<\/code> section. So for example in the Twenty Nineteen theme, the full <code>&lt;head&gt;<\/code> section\u00a0looks like this:<\/p>\n<div class=\"gist\" data-gist=\"6514b72970cddb32cd0299c2c9618b7a\" data-gist-file=\"Twenty Nineteen wp_head code.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/6514b72970cddb32cd0299c2c9618b7a.js?file=Twenty+Nineteen+wp_head+code.php\">Loading gist 6514b72970cddb32cd0299c2c9618b7a<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p><em>Note: If your theme doesn&#8217;t include this code, then I&#8217;d recommend getting another theme. A well-written theme will always include this, and if it doesn&#8217;t, who&#8217;s to say where else there may be problems?<\/em><\/p>\n<p>The last line before the closing <code>&lt;\/head&gt;<\/code> tag includes the <code>wp_head<\/code> hook. You can use this in your plugin to insert code at this point without having to edit your theme files.<\/p>\n<p>Why shouldn&#8217;t you edit the theme files, you might ask? Well, it&#8217;s a bad idea for two reasons:<\/p>\n<ul>\n<li>If the theme is updated in the future, you&#8217;ll lose any changes you&#8217;ve made to the theme files.<\/li>\n<li>A Google Analytics plugin is about functionality (i.e. what&#8217;s going on under the hood of your site) and not display (i.e. how your site looks and displays content), so it should be added via a plugin and not a theme.<\/li>\n<\/ul>\n<p>Once you know your theme is ready, the next step is to get hold of the Google Analytics tracking code.<\/p>\n<h3>Getting the Tracking Code<\/h3>\n<p>When you <a href=\"https:\/\/wpmudev.com\/blog\/bring-the-power-of-google-analytics-into-your-wordpress-dashboard\/\" target=\"_blank\" rel=\"noopener\">add your site to your Google Analytics account<\/a>, you&#8217;re given a tracking code that you need to insert into your site&#8217;s pages so that Google can track visitors to each page.<\/p>\n<p>If you have a Google account, you can access analytics data using your existing account. If you&#8217;re one of those rare people who\u00a0don&#8217;t have an account with Google yet, you&#8217;ll need to <a href=\"https:\/\/accounts.google.com\/signup\/v2\/webcreateaccount?hl=en&amp;flowName=GlifWebSignIn&amp;flowEntry=SignUp\" rel=\"noopener\" target=\"_blank\">set up a Google account<\/a>.<\/p>\n<p>Once you&#8217;ve done that, all you need to do is make sure that you have added your site to Google Analytics and then copy your tracking code from the Admin section.<\/p>\n<p>If you need help with this step, check out our tutorial on <a href=\"https:\/\/wpmudev.com\/blog\/google-analytics-tracking-code-wordpress\/\" target=\"_blank\" rel=\"noopener\">how to add Google Analytics tracking code to WordPress<\/a>.<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-670x670\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/09\/GA-tracking-code.png\" alt=\"Google Analytics tracking code.\" width=\"600\" height=\"307\" \/><figcaption class=\"wp-caption-text\">Google Analytics code example.<\/figcaption><\/figure>\n<p>Every site you add to Google Analytics has its own unique ID.\u00a0\u00a0Copy everything in the tracking code, including the opening and closing <code>&lt;script&gt;<\/code> tags. You will paste this code into your plugin shortly.<\/p>\n<h3>Creating the Plugin<\/h3>\n<p>Next, let&#8217;s create the plugin that we&#8217;ll be adding the tracking code to.<\/p>\n<p><em>Note: Make sure you <a href=\"https:\/\/wpmudev.com\/project\/snapshot\/\" target=\"_blank\" rel=\"noopener\">create a backup of your site<\/a> in case you do something accidentally that messes up your code.<\/em><\/p>\n<p>In your site&#8217;s <code>wp-content\/plugins<\/code> folder, create a new PHP file. Give it a memorable name: I&#8217;m calling mine <code>wpmudev-google-analytics.php<\/code>.<\/p>\n<p>In your code editor, open that file. Add the following to it:<\/p>\n<div class=\"gist\" data-gist=\"c5b3f136b7bd3d387e007be983bfee32\" data-gist-file=\"WPMU DEV Simple Google Analytics Plugin Header.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/c5b3f136b7bd3d387e007be983bfee32.js?file=WPMU+DEV+Simple+Google+Analytics+Plugin+Header.php\">Loading gist c5b3f136b7bd3d387e007be983bfee32<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>This tells WordPress that this is a plugin, who the author is, and what the plugin does. Edit your plugin and author details to match.<\/p>\n<p>Now below that code add the function that will contain the analytics tracking code:<\/p>\n<div class=\"gist\" data-gist=\"d5a4d73884f666dc7c5037bd506600d1\" data-gist-file=\"Empty function for tracking code.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/d5a4d73884f666dc7c5037bd506600d1.js?file=Empty+function+for+tracking+code.php\">Loading gist d5a4d73884f666dc7c5037bd506600d1<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<p>At the moment this is an empty function called <code>wpmudev_google_analytics()<\/code> and it&#8217;s attached to\u00a0the <code>wp_head<\/code> hook, which means that its content will be output whenever WordPress encounters that hook, i.e. at the end of the <code>&lt;head&gt;<\/code> section on each page.<\/p>\n<p>The next step is to copy the tracking code\u00a0to your function. Inside the function&#8217;s braces (and also inside the opening and closing PHP tags inside the braces), paste the tracking code you copied to your clipboard earlier.<\/p>\n<p>Your function will now look something like this:<\/p>\n<div class=\"gist\" data-gist=\"299204a328727b6a0896a12e646b770f\" data-gist-file=\"google analytics function.php\"><a class=\"loading\" href=\"https:\/\/gist.github.com\/299204a328727b6a0896a12e646b770f.js?file=google+analytics+function.php\">Loading gist 299204a328727b6a0896a12e646b770f<\/a><div class=\"gist-consent-notice\" style=\"display:none\"><p>Please <a href=\"javascript:Cookiebot.renew()\">update your cookie preferences<\/a> to enable preference cookies to view this gist.<\/p><\/div><\/div>\n<h3>Activating the Plugin<\/h3>\n<p>Now, save your plugin file and go to your site admin screens. Your WordPress Google Analytics plugin should be listed in the WordPress plugins screen:<\/p>\n<figure class=\"wp-caption aligncenter\" data-caption=\"true\"><img loading=\"lazy\" decoding=\"async\" class=\"attachment-670x670\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2015\/09\/wordpress-google-analytics-plugin.png\" alt=\"WordPress Google Analytics plugin google analytics plugin\" width=\"600\" height=\"56\" \/><figcaption class=\"wp-caption-text\">Your Google Analytics plugin in the WordPress plugin admin screen.<\/figcaption><\/figure>\n<p>Activate the plugin and go back to the Google Analytics site to verify your tracking code. You may have to wait a short while until Google picks up the new code, and then your code will be reported as working.<\/p>\n<p>Congratulations! Now you know how to\u00a0create a plugin for Google Analytics and WordPress.<\/p>\n<h3>Adding Google Analytics To WordPress &#8211; Recap<\/h3>\n<p>Learning how to add Google Analytics to WordPress is not hard but adding Google Analytics without a plugin in WordPress is a lot trickier.<\/p>\n<p>If you want to manually add a Google tracking code to your WordPress site, then you shouldn&#8217;t be adding it to your theme.<\/p>\n<p>A better way is to create a Google Analytics plugin for WordPress like the simple plugin we&#8217;ve just you how to create.\u00a0This plugin includes a function containing the tracking code provided by Google. You then attach that function to the <code>wp_head<\/code> hook. This hook is present in the <code>&lt;head&gt;<\/code> section of all well-written themes and lets you insert code without having to hack your theme.<\/p>\n<p>The best way if you&#8217;ve come this far and don&#8217;t want to mess around with code and just want a straightforward solution for viewing Google Analytics for your site, is to use a plugin like\u00a0<a href=\"https:\/\/wpmudev.com\/project\/google-analytics-for-wordpress-mu-sitewide-and-single-blog-solution\/\" target=\"_blank\">Beehive<\/a>. It&#8217;s not only a cinch to set up, but it includes Multisite integration and graphs and stats so you can get a quick overview of your site&#8217;s activity. And, of course, there are other <a href=\"https:\/\/wpmudev.com\/blog\/google-analytics-plugins\/\" target=\"_blank\" rel=\"noopener\">Google Analytics plugins for WordPress<\/a> that you can install.<\/p>\n<h3>Google Analytics Plugin Installed? Great! Here Are Your Next Steps<\/h3>\n<p>Once you&#8217;ve added Google Analytics done this, you&#8217;ll not only be able to track visitors to your site as part of your SEO strategy but if you offer WordPress management services to clients, you can <a href=\"https:\/\/wpmudev.com\/blog\/wpmu-dev-dashboard-now-with-built-in-analytics-and-plugin-white-labeling\/\" target=\"_blank\" rel=\"noopener\">integrate analytics with the WPMU dashboard<\/a> and add it to all your client reports (nice!).<\/p>\n<p>If you&#8217;re not a <a href=\"https:\/\/wpmudev.com\/hub\/my-websites\/#wpmud-member-benefits-sub-menu\" target=\"_blank\" rel=\"noopener\">member of WPMU DEV<\/a> yet, you can still <a href=\"https:\/\/wpmudev.com\/blog\/how-to-create-client-pdf-reports-with-wordpress-google-analytics\/\" target=\"_blank\" rel=\"noopener\">create client PDF reports with WordPress Google Analytics<\/a>.<\/p>\n<p>You can also <a href=\"https:\/\/wpmudev.com\/blog\/google-experiments\/\" target=\"_blank\" rel=\"noopener\">run Google Analytics experiments<\/a>, like A\/B tests, <a href=\"https:\/\/wpmudev.com\/project\/google-analytics-for-wordpress-mu-sitewide-and-single-blog-solution\/\" target=\"_blank\" rel=\"noopener\">track multisite analytics<\/a>, and a whole lot more!<\/p>\n<p>For more information on using Google Analytics with WordPress, see our\u00a0<a href=\"https:\/\/wpmudev.com\/blog\/google-analytics-guide\/\" target=\"_blank\" rel=\"noopener\">comprehensive guide to Google Analytics and understanding your WordPress site traffic<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding Google Analytics to your WordPress site will help you track who&#8217;s been visiting your site, what they&#8217;ve been looking at and how they got there. Inserting the tracking code can be tricky, so here&#8217;s a neat way to do it with your very own plugin.<\/p>\n","protected":false},"author":347011,"featured_media":155202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[11181,10958,9976],"tutorials_categories":[],"class_list":["post-145497","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-beehive","tag-google-analytics","tag-plugins-3"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/145497","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/347011"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=145497"}],"version-history":[{"count":43,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/145497\/revisions"}],"predecessor-version":[{"id":198342,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/145497\/revisions\/198342"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/155202"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=145497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=145497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=145497"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=145497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}