Tracking Logged In OpenCart Customers With Google Analytics Advanced Segments

This is part one of the blog series concentrating on integrating OpenCart and Google Analytics even tighter to get more data from your online store. See the complete list of the series here


There is a growing realisation within OpenCart store owners that they are missing out on some key Marketing data because of the way their store is set up. Recent OpenCart updates have made a trend towards showing customer activity and one of these reports is seeing when customers are logged into their account and what they looked at. Although this is undoubtedly a good move by the OpenCart team, isn’t data always better when it can be piled together rather than having separate reports all over the place.

So, over the next couple of blog posts I will be showing you how to customise how the Google Analytics data works with your OpenCart store and get richer information on your customers. In this blog post, we will be looking at how to make sure Google Analytics knows when your customers are logged in and from there you can drill down into what these logged in customers are looking at, if they made a purchase and what referral methods are sending qualified traffic to your online store.

As usual, you will need to have only a very basic understanding of coding (and a Google Analytics account obviously) to follow this OpenCart tutorial. Follow the steps and you will be up and running in no time!

Getting started

OK, the first thing we are going to have to do is take control of the Google Analytics tracking code away from OpenCart. So, go to your Google Analytics (I’ll abbreviate as GA from now on) account, Click “Admin” at the top right then choose the “Tracking Info” link tab along the middle of the screen. A box will pop up with your tracking code for the profile you’re currently in, just copy that code.

Next, go to your File Manager / FTP client of choice and open up the following file:

  • catalog/view/theme/[YOUR THEME]/template/common/header.tpl

Make a search for “” (without the quotation marks) which will take you to the end of the page header section. All you need to do is add in your GA tracking code just above the “” to add it in properly. See the image below:


Then, the next step is to stop OpenCart outputting the same code so login to your OpenCart admin panel and go to System->Settings->Edit->Server and head down to the bottom and make sure the “Google Analytics Code” box is empty as shown below:


The reason we put it into the template file first is to make sure you don’t miss out on any tracks as you’re moving things around. Now, you are all set up, the code is working as it was before but now we get to play around and add data captures to get richer information about our customers.

Tracking Logged In OpenCart Customers Using Google Analytics

Now, all we are going to do now is add in an extra PHP statement which uses the standard OpenCart logged in check to see whether or not it should pass an extra variable to the tracking code. You will need to add the following code after the set account push command but before the _trackPageview line as below:

customer->isLogged()){ ?>

     _gaq.push(['_setCustomVar', 1, 'logged', 'yes']);

So, the entire GA tracking code should look like this:

OK, now we’re almost there, we just need to set the segment key and values in your GA account and it will begin tracking logged in customers.

Setting up the Advanced Segment in Analytics for tracking Logged in OpenCart Customers

Log into your Google Analytics account and navigate to the “Audience Overview” page of the profile you want to set the Advanced Segment up for. Just beneath the title you will see a link which says “Advanced Segments”, click it and select the “+ New Segment” option. You will see a form as below:

Give your segment a name which you will easily understand, in my case I will choose “Logged In Visitors”, no chance of me forgetting what it is! On the top line make sure the drop down is set to “Include” and then “Custom Variable (Key 1)”, then “Exactly Matching” and type “logged” (without the quotation marks) into the text box afterward.

Then you need to add an AND statement which links it to the value so click the “Add an AND statement” link and a new line will appear. The first drop down should be “Include”, then “Custom Variable (Value 1)”, then “Exactly Matching”, then type “yes” (without the quotes) into the text box following.


Then click “Save Segment” and away you go. You can apply, or remove, the segment from changing your data anytime by clicking again on the “Advanced Segments” link on the Overview page and choosing the “Logged In Visitors” checkbox on the right hand side. Once it is applied you can begin looking through the data and comparing how registered customers interact with your customers as opposed to the general visitors in general.

As always, leave a comment if you get stuck or want to add/ask anything.

Leave a Reply