Install Google Analytics on your WordPress site

How to Install Google Analytics in WordPress

There are many different ways to setup Google Analytics in WordPress. We will show you three different methods where the first option is the easiest and the last being the hardest.

You can choose the one that best suits your needs.

1. Google Analytics for WordPress by MonsterInsights

MonsterInsights is the most popular Google Analytics plugin for WordPress. Over 1 million websites use it including the likes of Bloomberg, PlayStation, Zillow, and more.

It is the easiest and by far the best way to add Google Analytics to WordPress (for all users beginners and experts).

MonsterInsights has both a free version and the Pro version. In this tutorial, we will be using the MonsterInsights free version.

You can use the MonsterInsights Pro version if you want more advanced features like Ecommerce tracking, Ads tracking, Author tracking, etc. The process of setting them up is the same.

Let’s get started.

The first thing you need to do is install and activate the MonsterInsights plugin.

For more details, and if you want to learn how to install and manage WordPress plugins, see our WordPress basics course.

Upon activation the plugin will add a new menu item labeled ‘Insights’ to your WordPress admin menu. You need to visit Insights » Settings to configure the plugin settings.

On this screen, you need to click on ‘Authenticate with your Google account’ button to connect Google Analytics with your WordPress site.

authenticate WP

This will bring up a popup where you will be able to authenticate with your Google Account.

authenticate Google

Click on ‘Next’ to continue.

Now you need to click on ‘Click to get Google code’ button.

Get authentication code

This will bring up another popup with the message ‘MonsterInsights would like access to your Google Analytics data’.

Click on the ‘Allow’ button to continue.


The authentication popup will now show you a line of code which you need to copy.

authentication code

Next, you need to enter this code in the MonsterInsights popup and then click on ‘Next’ to continue.

enter google code

The final step is to select the profile you want to track. You need to select your website here and then click on the next button to continue.

select google pofile

That’s all you have successfully installed and setup Google Analytics on your WordPress site.

Remember, it will take Google Analytics sometime before showing your stats.

The best part about MonsterInsights is that you can view your Google Analytics report inside your WordPress dashboard. Simply visit Insights » Reports page to checkout a quick overview of your analytics data.


You can go also go in the Tracking tab to customize the tracking settings based on your needs:

insights tracking

Note: MonsterInsights was formerly known as Google Analytics for WordPress by Yoast.

2. Insert Headers and Footers Plugin

This method is not as not as good as MonsterInsights because you will not be able to do advanced tracking configuration, and you will not be able to view Google Analytics data in your WordPress dashboard.

First, you need to install and activate the Insert Headers and Footers plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit Settings » Insert Headers and Footers page. Here you need to paste the Google Analytics code that you copied in step 4 into the headers section.

adding code to header footer in wrdpress
Don’t forget to click on the save changes button to store your settings.

3. Install Google Analytics in WordPress Theme

This method is for advanced users only who are familiar with code. It is somewhat unreliable because your code will disappear if you switch or update the theme. We almost never recommend using this method.

If this is your first time adding code to your WordPress files, then you should check out our guide on how to copy paste code snippets in WordPress.

Add code in header.php file

Simply edit the header.php file in your WordPress theme and paste the code you copied in step 4 right after the <body> tag.

Don’t forget to save your changes and upload the file back to your server.

Add via Functions File

You can also add Google Analytics tracking code to WordPress functions file. It will then automatically add code to every page on your WordPress site.

You will need to add this code to your theme’s functions.php file.

1 <?php
2 add_action('wp_head', 'wpb_add_googleanalytics');
3 function wpb_add_googleanalytics() { ?>
5 // Paste your Google Analytics code from Step 4 here
7 <?php } ?>

Viewing Reports on Google Analytics Website

Google Analytics is capable of showing you a treasure of data collected from your stats. You can view this data by visiting your Google Analytics dashboard and clicking on the reporting tab.

Google Analytics reports

You will see the built-in Google Analytics reports in the left column. Each section is divided into different tabs and clicking on a tab will expand it to show more options.

  • Audience tab will show reports to help you understand your users.
  • Acquisition reports explore where your users came from.
  • Behavior reports summarize what your users do after they arrive on your site.
  • Conversion reports show how well you’re doing against your goals.

We hope this article helped you learn how to install Google Analytics in WordPress.

My Greek characters are all garbled and unreadable

When creating WordPress pages and posts you are not confined to use only the English language and character set. In fact, any language can be used and in Cyprus, even if the majority of our reader do understand and speak English, sometimes you’d prefer publishing your content in Greek.  Sometimes after a database update things may have gone wrong and your site becomes unreadable.

Fix garbledgreek characters in WordPress

Fix garbled Greek characters in WordPress

When your Greek text in WordPress shows suddenly all garbled and unreadable there is Character Encoding Mismatch issue.

How to Fix a character encoding mismatch problem in WordPress

1) You need to edit the ‘wp-config.php’ file. Either use your online editor or open the ‘wp-config.php’ file in a separate text editor such as notepad (the wp-config.php file can be found in the root directory of your WordPress installation). It is always a good idea to first make a backup of the file.

2) Locate the following two lines and comment them out:

define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

After you commented them out, they should look like :

//define('DB_CHARSET', 'utf8');
//define('DB_COLLATE', '');

Step 3) Now upload (or save if you use your online editor) the updated ‘wp-config.php’ file to your hosting platform (overwriting the old wp-config.php file, but keep the back, just in case….).

Character encoding problems can happen after a database upgrade or other interventions on the site (plugins, etc….).

Add post shortcodes to WordPress pages

Displaying Recent Posts in WordPress Using Shortcode

Adding recent posts to a WordPress sidebar is fairly easy, but what if you wanted to show recent posts inside a WordPress post or page? The easiest way to display recent posts in WordPress posts and pages is by using shortcodes.

You’d begin by installing and activating the Display Posts Shortcode plugin. It works out of the box and there are no settings you need to configure.

Simply edit a post or page where you want to display your recent posts. Next, use the shortcode [display-posts] with your own parameters inside the post. The plugin offers a whole range of parameters that you can use with the shortcode. Here are some examples:

Display 5 recent posts with thumbnails and excerpt

1 [display-posts posts_per_page="5" image_size="thumbnail" include_excerpt="true"]

Display recent pages instead of posts

1 [display-posts posts_per_page="5" post_type="page"]

Change the order to title instead of date.

1 [display-posts posts_per_page="5" orderby="title"]

Display recent pages under a specific parent page.

1 [display-posts posts_per_page="5" post_type="page" post_parent="5"]

For a full list of parameters visit the plugin’s documentation.

You can also use these shortcodes inside a text widget, but first you will need to enable shortcodes in your text widgets by adding this code to your theme’s functions.php file or a site specific plugin.

1 add_filter('widget_text', 'do_shortcode');


Watch the video about displaying recent posts on pages and posts inWordpress:

What shortcodes are included in WooCommerce

Page Shortcodes:

  • [woocommerce_cart] – shows the cart page
  • [woocommerce_checkout] – shows the checkout page
  • [woocommerce_order_tracking] – shows the order tracking form
  • [woocommerce_my_account] – shows the user account page

In most cases, these shortcodes will be added to pages automatically via our onboarding wizard and do not need to be used manually.


Used on the cart page, the cart shortcode displays cart content and interface for coupon codes and other cart bits and pieces.

Args: none



Used on the checkout page, the checkout shortcode displays the checkout process.

Args: none


Order Tracking Form

Lets a user see the status of an order by entering their order details.

Args: none


My Account

Shows the ‘my account’ section where the customer can view past orders and update their information. You can specify the number of orders to show. By default, it’s set to 15 (use -1 to display all orders.)


     'current_user' => ''
Current user argument is automatically set using get_user_by( ‘id’, get_current_user_id() ).

The following shortcodes can be used anywhere you want:

Recent Products

Lists recent products – useful on the homepage. The ‘per_page’ shortcode determines how many products to show on the page and the columns attribute controls how many columns wide the products should be before wrapping.


     'per_page' => '12',
      'columns' => '4',
      'orderby' => 'date',
      'order' => 'desc'
[recent_products per_page="12" columns="4"]
To learn more about the default ‘orderby’ parameters, see: WordPress Codex Class Reference

Featured Products

Works the same as recent products but displays products that have been set as “featured.” In this example, the shortcode is saying: Show 12 featured products in 4 columns.


     'per_page' => '12',
      'columns' => '4',
      'orderby' => 'date',
      'order' => 'desc'
[featured_products per_page="12" columns="4"]


Show a single product by ID or SKU.

[product id="99"]
[product sku="FOO"]

*If the product isn’t showing, make sure it isn’t set to Hidden in the Catalog Visibility.

To find the Product ID, go to the Products screen, hover over the product and the ID appears as shown below.



Show multiple products by ID or SKU. Make note of the plural ‘products.’


      'columns' => '4',
      'orderby' => 'title',
      'order' => 'asc'
[products ids="1, 2, 3, 4, 5"]
[products skus="foo, bar, baz" orderby="date" order="desc"]

*If the product is not showing, make sure it is not set to Hidden in the Catalog Visibility.

Add to Cart

Show the price and add to cart button of a single product by ID.


      'id' => '99',
      'style' => 'border:4px solid #ccc; padding: 12px;',
      'sku' => 'FOO'
[add_to_cart id="99"]

Add to Cart URL

Echo the URL on the add to cart button of a single product by ID.


      'id' => '99',
      'sku' => 'FOO'
[add_to_cart_url id="99"]

Product Category

Show multiple products in a category by slug.

Go to: WooCommerce > Products > Categories to find the slug column.
Go to: WooCommerce > Products > Categories to find the slug column.


     'per_page' => '12',
      'columns' => '4',
      'orderby' => 'title',
      'order' => 'asc',
      'category' => ''
[product_category category="appliances"]

Product Categories

Display product categories loop


      'number' => 'null',
      'orderby' => 'title',
      'order' => 'ASC',
      'columns' => '4',
      'hide_empty' => '1',
      'parent' => '',
      'ids' => ''

The number field is used to display the number of products and the ids field is to tell the shortcode which categories to display.

[product_categories number="12" parent="0"]

Set the parent parameter to 0 to only display top level categories. Set IDs to a comma separated list of category IDs to only show those.

To find the Category ID, go to the Product Categories screen, hover over the category and the ID appears in the URL.

Product Page

Show a full single product page by ID or SKU.

[product_page id="99"]
[product_page sku="FOO"]

Sale Products

List all products on sale.


     'per_page' => '12',
     'columns' => '4',
     'orderby' => 'title',
     'order' => 'asc'

[sale_products per_page="12"]

Best-Selling Products

List best-selling products on sale.


     'per_page' => '12',
     'columns' => '4'

[best_selling_products per_page="12"]

Related Products

List related products.


     'per_page' => '12',
     'columns' => '4',
     'orderby' => 'title'

[related_products per_page="12"]

Top Rated Products

List top-rated products on sale.


     'per_page' => '12',
     'columns' => '4',
     'orderby' => 'title',
     'order' => 'asc'

[top_rated_products per_page="12"]

Product Attribute

List products with an attribute shortcode.


     'per_page' => '12',
     'columns' => '4',
     'orderby' => 'title',
     'order' => 'asc',
     'attribute' => '',
     'filter' => ''

[product_attribute attribute='color' filter='black']

The ‘per_page’ Argument

Note: the ‘per_page’ shortcode argument will determine how many products are shown on a page. This will not add pagination to the shortcode.

Troubleshooting Shortcodes

If you correctly pasted your shortcodes and the display looks incorrect, make sure you did not embed the shortcode between <pre> tags. This is a common issue. To remove these tags, edit the page, and click the Text tab:

Remove Pre Tags from Shortcode

Sorting Products by Custom Meta Fields

In many shortcodes like:

  • [recent_products]
  • [featured_products]
  • [products]
  • [product_category]
  • [sale_products]
  • [top_rated_products]
  • [product_attribute]
  • [related_products]

you can choose to order products by the following values

  • menu_order
  • title
  • date
  • rand
  • id

using the “orderby” attribute, for example:

[products skus=”foo, bar, baz” orderby=”date” order=”desc”].

But you can also sort products by custom meta fields using the code below (in this example we order product by price):

add_filter( 'woocommerce_shortcode_products_query', 'woocommerce_shortcode_products_orderby' );

function woocommerce_shortcode_products_orderby( $args ) {

    $standard_array = array('menu_order','title','date','rand','id');

    if( isset( $args['orderby'] ) && !in_array( $args['orderby'], $standard_array ) ) {
        $args['meta_key'] = $args['orderby'];
        $args['orderby']  = 'meta_value_num'; 

    return $args;

You need to place this snippet in functions.php in your theme folder and then customize by editing the meta_key.