Ideally, your theme provides a place to paste the google analytics tracking code and that’s all you have to do to get started. Alternatively, you can install a plugin which also makes it very easy to install google analytics. However, if your theme doesn’t provide a spot to paste the google analytics tracking code (the twenty sixteen theme does not) and if you prefer not to use a plugin, then the best way to install google analytics is to use a child theme with some code to run google analytics. Note: you might find instructions on the internet explaining how to paste the google tracking code directly into the header.php file. This is super easy to do, but don’t waste your time with this option. Your tracking code will be overwritten every time you update WordPress.

In brief, here’s how to use a child theme to run google analytics: The first step is to create a child theme folder. Then you will create two files: functions.php and style.css, then paste some code into these two files. Note: there’s one caveat to using this option that I’m not sure how to resolve: If your theme has multiple style sheets aside from style.css (for example, styles-custom.css, etc.) then I’m not sure how to code that into the functions.php file shown below. I think most themes just run off of style.css, but I’m not sure.

Before getting started I’d suggest reading the WordPress help file on child themes.

Here are the step by step instructions:

  • Sign up for Google Analytics and get your google tracking code. It will start with UA, followed by a series of digits, like this: UA-12345678-2. You can find good instructions elsewhere for to help you through the sign up process.
  • Create a folder for your child theme and name it accordingly. Note: in this example, I’m creating a child theme for the twentysixteen theme, so I named the folder: twentysixteenchild. Here’s how to do it:
  • Go to your webhosting account >> cpanel >> go to file manager (if you’re using SiteGround) or equivalent if using another hosting company >> public_html folder >> select the folder containing your website’s name >> wp-content folder >> themes folder
  • Create a new folder within the “themes” folder called twentysixteenchild
  • While you are in the “twentysixteenchild” folder, create a new file (the button to create a new file is at the top left of the page and is called “New File”) and name the file: style.css.
  • Now paste  all of the following text shown in red in your style.css file and save it:

/*
Theme Name: twentysixteenchild
Description: Child theme for the parent theme Twenty Sixteen.
Author: Tim
Author URI: notapplicable
Template: twentysixteen
Version: 1
*/

*** Note: The various answers you put for each line above are not important except the one that says: “Template”. You have to put the correct name of your main theme here. This is what I read in the link I provided above, but it doesn’t make sense to me because in my understanding the /* and */ that flank the text above denote a comment and the any code between these characters isn’t read. So I’m not sure why it matters, but to be safe, make sure that you put the name of the  main theme beside where it says: “Template”.

  • 7. Now, while still in the “twentysixteenchild” folder, create a file and name it: functions.php.
    • Note: to create a file, first make sure you’re in the folder you want to be in (the folder hierarchy is on the left side of the page), then click “new file” from the list of options in the row of buttons along the top of the page.
  • 8. Paste all of the code shown below in red into the functions.php file and save it.

<?php

add_action( ‘wp_enqueue_scripts’, ‘mychildtheme_enqueue_styles’ );
function mychildtheme_enqueue_styles() {
wp_enqueue_style( ‘parent-style’, get_template_directory_uri() . ‘/style.css’ );
}

function awesome_enqueue_styles() {

$parent_style = ‘parent-style’;

wp_enqueue_style( $parent_style, get_template_directory_uri() . ‘/style.css’ );
wp_enqueue_style( ‘child-style’,
get_stylesheet_directory_uri() . ‘/style.css’,
array( $parent_style )
);
}
add_action( ‘wp_enqueue_scripts’, ‘awesome_enqueue_styles’ );

/*
NOTE: The above code says: use the style.css file from the parent theme (which in this example is twentysixteen)

*/

add_action(‘wp_head’, ‘wpb_add_googleanalytics’);
function wpb_add_googleanalytics() { ?>
<script async src=”https://www.googletagmanager.com/gtag/js?id=UA-12345678-2″></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());

gtag(‘config’, ‘UA-12345678-2’);
</script>

<?php }

/*
NOTE: the above code adds google analytics to every page of your website
*/

  • Now find the two spots in the code above, where it says: UA-12345678-2 and change it to your google analytics code.
  • Now sign into WordPress and go to your dashboard and activate your child theme, you should be able to see it there under: Appearance >> Themes. Google analytics should now be running, though according to them, it can take up to 48 hours to start tracking.
  • Note: if you upload a 1200px by 900px picture to your child theme folder (twentysixteenchild in this example) and name it: screenshot.png, then this picture should show up in your WordPress dashboard, as the image for your child theme, when you go to: Appearance >> Themes. Note: this didn’t work for me but I couldn’t be bothered changing the pic resolution, so that might have been the problem.