How to add Google Analytics to WordPress

Add your Google Analytics tracking code to every page on your WordPress in 4 simple steps.

Overview

  1. Check your theme
  2. Prepare your Google Analyics
  3. Create plugin
  4. Activate plugin

Checklist:

  • Time needed: 20-40min.
  • Programs you need: A running WordPress, a FTP client like FileZilla (free) and a text editor or a coding tool of your choice.
  • Access you need: Administration rights to the FTP server and a Google Analytics account.

In some themes you can simply include the Google tracking code snippet Google Analytics provides to you to your websites <head></head> under ‘Appearance > Theme Options’ (label may vary). You may also be required to activate custom header codes and save the changes, before the code is included to each page on your site.

If there is no such option however, you have different possibilities to add the tracking script to your WordPress. You can install a plugin like Analytics+ that gets the job done for you and also gives you access to your analytics dashboard within your WordPress. If you want to keep things separated however, this tutorial is for you.

This tutorial is a shorter recreation of Rachel McCollins work.

Let’s get started:

1. Check your theme

Log into your FTP client and find the folder where the running theme is installed to. (‘/websites/wordpress/wp-content/themes/theme_name/’) Drag the header.php file to your desktop and open it. Make sure it contains <?php wp_head(); ?> in its head.

Themes you downloaded from WordPress directly, or WordPress default themes should have the wp_head hook included. You should consider choosing a different theme, if wp_head is not included.

Note: Do not include the tracking script here already. If you’d do, you’ll firstly lose your changes whenever the theme is updated and secondly, the very same tracking code would be used in EVERY page using this theme. So in case you manage multiple sites with one WordPress using the same theme, Google Analytics will merge the clicks from different sites.

If your theme header.php contains the marked line, you're good.
If your theme header.php contains the marked line, you’re good to go.

2. Prepare your Google Analytics

Login to your Google Analytics and get your tracking code.

Create a new account: Go to ‘Admin’ and click ‘Account > Create new account’, fill out the form and hit Get Tracking ID. Copy the shown website tracking script. This works similar, if you want to create a new property within an existing. Click ‘Property > Create new property’ and follow the description.

create new google analytics account
Create a new account on Google Analytics
create new google analytics account 1
Fill out the forms with your information.

Use an existing account: Go to ‘Admin’ choose the account you want to use. Click ‘Tracking Info’ within the property column and hit Tracking code. Copy the code.

You tracking-ID and website tracking code
You tracking-ID and website tracking code.

3. Create plugin

Create a text editor file on your desktop via right click ‘New > editor’ and paste the tracking code. Next, copy

/*
Plugin Name: My Google Analytics Plugin
Plugin URI: http://autario.com
author: autario.com
Description: This plugin adds your Google Analytics tracking code to the head of any theme you use, as long as it allows wp_head hooking.
Version: 1.0 */

function my_google_analytics() { ?>

DELETE THIS LINE AND COPY YOUR GOOGLE ANALYTICS TRACKING CODE HERE!

add_action( 'wp_head', 'my_google_analytics', 10 );

and paste it to the very beginning of the document you copied your tracking code to. Now cut your tracking code, and put it INSTEAD of the line saying ‘Delete this line and copy your Google Analytics tracking code here!’.

Save the file as my_google_analytics.php and upload it to the folder ‘/websites/wordpress/wp-content/plugins‘ on your server.

Your code should look similar to this now.

google analytics plugin

4. Activate plugin

Activate the plugin under ‘Plugins’, and you’re done. Check if it worked, by clicking Sent test traffic on your Google Analytics account. You find it on the same page where you get your tracking code. Go to Home, choose your account and adjust the time frame of your overview (you have to include the actual date, otherwise it will take a day to show up) or click on ‘Real Time > Overview’, if you just visited your site.

Note: If you excluded your own IPs from your analysis, which I strongly recommend, since you do not want to measure your own behavior on your site, and your traffic will highly manipulate the data (unless you have millions of clicks per day), you might need to use devices with different IPs. F.e. your smartphone via mobile web.

Share this

WordPress Multisite Domain Mapping Tutorial

This is a detailed step-by-step how-to guide to use one single WordPress installation for different blogs and even for different domains.

At the end of this tutorial you'll organize a list of different sites with their own domain with one single WordPress installation.
At the end of this tutorial you’ll organize a list of different sites with their own domain in one single WordPress installation environment.

Overview

  1. Install WordPress
  2. Create backup
  3. Allow multisite
  4. Install domain mapping plugin
  5. Prepare for mapping
  6. Setup mapping
  7. Prepare domains
  8. Manage sites
  9. Start mapping
  10. Happy blogging

Checklist

  • Time needed: 30-60min.
  • Files you need: WordPress (if not yet installed), WordPress MU Domain Mapping.
  • Programs you need: FileZilla (free) or any other FTP client. Text editor or any other programming software of your choice like Dreamweaver or Microsoft Visual Studio. The second one is free for students over MSDN (Microsoft Developer Network). Definitely worth checking out.
  • Access you need: Administration rights to the FTP server, your provider account and WordPress site, if preinstalled.

The tutorial is an updated version of Lars Mielkes in-depth tutorial in english, solving for the problem of unreachable mapped sites and ‘cookie errors’ when trying to log into your newly mapped domain, I experienced while following different tutorials. If you’re here because you had the same problem, this tutorial is for you (solution in step 8).

If you only want to have multiple sites without mapping them to their own domain, follow along but drop steps  4-7, 9 and 10. From now on I waive every unnecessary background info.

Let’s get started

1. Install WordPress

First things first, install WordPress, if you haven’t already. There are >3 different options to install WordPress on your server.< Important: Directly install WordPress to one of your domains. Don’t use a subdomain. This will keep you out of trouble later on. Make sure that you want to keep WordPress on the domain you want to install it to, since if you delete WordPress from the main site later on, all other mapped domains will go blank. You can only delete WordPress from a mapped domain without effecting the other sites.

2. Create backup

Your main domain should show a default WordPress site now, when opened in your browser. Open FileZilla and log into your FTP. Find the WordPress folder under the path you installed it to. (F.e. under /websites/wordpress) Scroll down to find the file wp_config.php. Copy it to your desktop via drag and drop. Also create a new folder on your desktop where you additionally copy wp_config.php into. (In case it doesn’t work out, you’re better off to have the original saved!).

3. Allow multisite

Now open the file wp_config.php (the one on your desktop) with the text editor, by right clicking and ‘open with > text editor’. Copy the line

define('WP_ALLOW_MULTISITE', true);

and paste it into wp_config.php BEFORE the line /* That’s all, stop editing! Happy blogging. */. Save it and upload it to your server. Overwrite the existing file. Now go to your website and hit ‘login’ or add /wp-admin to the URL (www.yourwebsite.com/wp-admin). Login with your WordPess account. If you’ve been logged in, log out and back in again. Now go to ‘Tools > Network Setup’, type in the requested information and hit install.

before and after allow wp multisite
Before and after allowing multisite.

Next, as described on the site, open the file wp_config.php you altered already, copy

define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'yourwebsite.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

and paste it again BEFORE the line /* That’s all, stop editing! Happy blogging. */, save it and upload it to your WordPress folder on the server again, rewriting the existing file. (Make sure you change the URL to the one that applies.)

Find the file .htaccess in the same folder and copy it to your desktop. Also save a copy to the new folder you created already for the original wp-config.php file. Open the file .htaccess (the one from your desktop) and copy

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

Delete the overwrite rules defined in the original file. If you newly installed WordPress due to this tutorial, you can simply delete everthing between the IfModule mod_rewrite.c and the IfModule command. Save the file and upload it to your server. Overwrite the existing file.

Old and new .htacess file.
Old and new .htaccess file.

Now log out and back into WordPress. If you were successfull, your admin environment should look like this.

New Network Admin environment.
New Network Admin environment.

4. Install domain mapping plugin

If you haven’t already, download WordPress MU Domain Mapping.

Go back to your WordPress and click ‘My Sites > Network Admin’ to follow along. Here you have super admin rights. If you stay within the admin environment of your site, you won’t be able to install plugins anymore. Now click ‘Plugins > Add New’ and hit upload file. Find the WordPress MU Domain Mapping zip file you downloaded and hit ‘install now’. (Alternatively you can search ‘WordPress MU Domain Mapping’ in the search bar and install it from there.)

Go to ‘Plugins > Installed Plugins’ and activate the MU Domain Mapping plugin for the network, and log out afterwards.

Before and after activating plugin.
Before and after activating plugin.

5. Prepare for mapping

Open your FTP client again and find the file sunrise.php under ‘wordpress > wp-content > plugins > wordpress-mu-domain-mapping’. Copy it to your desktop only to drag it into the ‘wp-content’ folder on your server.

Open wp_config.php on your desktop again and add

define( 'SUNRISE', 'on' );

again BEFORE the line /* That’s all, stop editing! Happy blogging. */. Save the file and overwrite the original on your server again.

How your wp_config.php file should look like. (Never mind if your program displays the code in different color)
How your wp_config.php file should look like. (Nevermind if your program displays the code in different colors)

6. Setup mapping

Log into your WordPress and go to the Network Admin again. Under ‘Settings’ you’ll now find the options ‘Domain Mapping’ and ‘Domains’. Click ‘Domain Mapping’ and type in your Server IP Address.

You’ll find your server IP by entering the domain you installed WordPress on here. Copy and paste it into the form. If you choose to go for the IP form leave the CNAME option blank. Otherwise the CNAME info is used.

Choose your Domain Options. Option 2 and 3 worke fine. Click save.

domain mapping

7. Prepare domains

Now log into your account at the website of your provider where your domains are registered and direct the domains you want to map with WordPress to the folder where WordPress is installed. It should be the same directory your main site is pointing to. F.e. ‘/websites/wordpress’. These changes may take a few moments, depending on your provider. Test if it worked by entering the domains into your browser. This should lead you to a default WordPress site of your main domain.

8. Manage sites

Now you can start mapping blogs to independent domains from your single WordPress installation. Log into WordPress and go to ‘My Sites > Network Admin’, and then to ‘Sites’. If you used WordPress before, you might have different blogs listed under sites already that you want to host by its own domain. If there is only your main site displayed under ‘All Sites’, click ‘Add New’ and type in the requested information. Hit ‘Add Site’.

I recommend you assign yourself as admin, even if the site is for someone else. You can add the other user later via editing the site and remove yourself as admin. The new site should be displayed under ‘All Sites’.

Depending on how your multisite network was installed, domain based (with subdomains, f.e. subdomain.yourwebsite.com) or path based (with subdirectories, f.e. yourwebsite.com/subdirectory), you’ll have to follow the steps that apply to you. (I missed where you can choose before installing. I installed it multiple times, but didn’t find out yet. If you know more, let me know. I only found a tutorial on how to switch afterwards.)

If you can only add subdomains (subdomain.yourwebseite.com) in WordPress, you have to add the exact same subdomain in your provider account and point it to the directory where wordpress is installed (f.e. /websites/wordpress). You can delete this subdomain at your provider account again after everything is set up. I will tell you when.

If you, however, can add subdirectories to your main domain (yourwebsite.com/subdirectory), you’re lucky and you do not have to go into your provider account.

Add a random subdomain to WordPress. BUT add the same to your domain provider.
Add a random subdomain to WordPress. BUT add the same to your domain provider.

9. Start mapping

To map the site to its own domain, either log in to your new site and click ‘Tools > Domain Mapping’ and enter your desired domain. Check ‘Primary domain for this blog’ and click Add. If you choose www.yourwebsite.com as the primary site, also add yourwebsite.com (without the www.). Otherwise people who do not type www. in their browser might have difficulties reaching you.

Alternatively go to the Network Admin and add the domain under ‘Settings > Domains’. Take the side ID from the list under ‘Sites > All Sites’. If it is the first site after your main site, it has the ID 2. This, however, is prone to errors, if you’ve had different sites you deleted already, so I recommend you go with the first option.

10. Happy blogging

After that is being done, and your site is mapped to its domain, you will have to log in again, even though you’re the super admin.

If you can’t log in because you experience a ‘cookie error’, try to log in with a wrong made up admin name and password. Than try to log in with your super admin user name and password. That fixed that issue for me and it never showed up again. If this doen’t help delete your cookies or try to log in with a different browser.

Go back to the Super Admin network overview to ‘All Sites’ and hover your cursor over your mapped site. Click edit and change the URL to the mapped domain under Info. Hit Save Changes. Check if the URLs under Settings are also changed and update the Blogname and Blogdescription there. Scroll all the way down and click Save Changes again. If you want to add other users and remove yourself from some sites, like mentioned before, you can do that here too.

If your subdomain.yourwebsite.com now leads to the old default site, when opened via your browser, you’re good to delete the subdomains from your provider again.

To work with other users on blogs, you can also now create different users and assign them to different blogs.

Now stop editing, and happy blogging :)

Share this