Google Analytics for WordPress
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Google Analytics offers detailed statistics related to visitor traffic and sales for your website, allowing you to better know your audience. It can be beneficial to any website owner interested in growing their visitor base.
This guide provides three ways to add Google Analytics to WordPress: By directly adding the analytics code to your theme and two plugin options. Prior to using this guide, you should have completed our Manage Web Content with WordPress guide and have a fully configured WordPress website set up.
NoteThe steps in this guide require root privileges. Be sure to run the steps below asroot
or with thesudo
prefix. For more information on privileges, see our Users and Groups guide.
Sign Up for Google Analytics
Prior to adding Google Analytics to your website, you need to sign up for a Google Analytics account.
Navigate to the Google Analytics website, then click the Access Google Analytics button at the top right.
Click Sign Up.
Be sure the Website option is selected, then enter your account information as desired. Be sure that your website URL is accurate.
Press Get Tracking ID, and read through and accept the Google Analytics Terms of Service.
Make sure to note the Tracking ID and tracking code, you will use them again in this guide.
You are now set up with Google Analytics and can decide how to proceed with adding the code to your WordPress website: Through your theme or through a plugin.
Add Google Analytics Through a Theme
WordPress uses PHP includes, so adding a Google Analytics code is as easy as altering a single file in your WordPress theme.
NoteThis guide assumes you have configured your LAMP server as described in our guides, with your publicly accessible directory located at something similar to/var/www/example.com/public_html
. Replace all instances ofexample.com
with your own domain information.
Navigate to your theme directory:
cd /var/www/example.com/public_html/wp-content/themes
Use the
ls
command to see your list of theme folders. Note the folder name of the theme are you currently using:ls
Navigate to the folder that denotes the current theme you are using. In this example we are using the default Twenty Fifteen theme:
cd twentyfifteen
Open
header.php
and add your Google Analytics tracking code underneath the<body <?php body_class(); ?>>
tag:- File: /var/www/example.com/public_html/wp-content/themes/header.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * Google Analytics code block */ <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-00000000-0', 'auto'); ga('send', 'pageview'); </script>
Note
If you copy the above code, replace
UA-00000000-0
with your tracking ID.At this time you may want to consider enabling the demographics feature of Google Analytics. If you decide to do so, you will need to add an additional line of code to your JavaScript in the steps below. Insert the following between the lines containing
ga('create', 'UA-00000000-0', 'auto');
andga('send', 'pageview');
:ga('require', 'displayfeatures');
Should you decide to disable the demographics feature at a later date, simply remove the above code.
To determine the success of the above process, open your WordPress website in a browser, then right click and select View Page Source. You should see the Google Analytics code inserted below the
<body>
tag. It is highlighted in the below example:
Congratulations! You have added Google Analytics to your WordPress website. It may take up to twenty-four hours for any data concerning your website to show up on Google Analytics. Please note that if you change the theme you will have to redo the above steps.
Add Google Analytics Through a Plugin
This section explores adding Google Analytics to a WordPress website through two popular plugins. These plugins both add Google Analytics dashboards to your administration interface and insert the tracking code into your theme. If you have already added Google Analytics through the above method, these plugins can complement that process by adding in-dashboard features, should you desire to view your analytics within WordPress itself.
If you do not want to incorporate an added dashboard to WordPress, and would rather monitor your visitors and demographics from the Google Analytics website, consider using the method of adding your code through your chosen theme, as shown above.
Google Analytics Dashboard for WP
The Google Analytics Dashboard for WP is a comprehensive WordPress plugin that incorporates Google Analytics to your dashboard home, allowing you to track changes in visitors from the moment you log in. It is a powerful plugin that also offers both backend and frontend features allowing you to share the Google Analytics results with everyone from visitors to blog contributors.
Prior to installing the Google Analytics for WP plugin you will need to add PHP Curl to your Linode. Log in to your server through SSH to update and install PHP Curl:
apt-get update && apt-get install php5-curl
Log in to your WordPress dashboard and navigate to the Add New link under the Plugins menu. Search for, download, and enable Google Analytics for WP. A Google Analytics link will be added to the navigation pane to the left.
Click on the Google Analytics link and select Authorize Plugin. It will ask for an access code, which you can acquire by clicking the Get Access Code link. You will need to log in to your Google account and allow the plugin to view your Google Analytics data.
Copy the resulting code and insert it in the appropriate text field. From here, you can select the appropriate domain and alter other settings as needed.
If you have not already inserted the tracking code within your website, make sure Tracking Options under the Tracking Code link is set to Enabled. Otherwise, this can be disabled.
Google Analytics Dashboard for WP has now been set up and configured. You can proceed to use WordPress as usual. It may take up to twenty-four hours for any data concerning your website to show up on Google Analytics.
Google Analytics by Yoast
Google Analytics by Yoast inserts a Google Analytics section to your WordPress dashboard, and adds your tracking code to your website. Google Analytics by Yoast allows for wider customization of your analytics within your WordPress administration interface, but cannot be shared with other authors or visitors of your WordPress website.
Log in to your WordPress dashboard and navigate to the Add New link in the Plugins menu. Search for, download, and extract Google Analytics by Yoast. Once activated, a Google Analytics link is added to the left navigation pane.
Click on the Analytics link. You will need to authenticate with your Google account by selecting the authenticate button, logging in to your Google account and allowing Google Analytics by Yoast to view your Google Analytics data.
Copy the resulting code and paste it into the box. From here, select the domain you are using Google Analytics for and alter other settings as needed. Be aware that if your domain information is not entered correctly, this plugin does not work.
Google Analytics by Yoast has now been set up. It may take up to twenty-four hours for any data concerning your website to show up on Google Analytics.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on