Analytics, Google Tag Manager, Shopify

How to Enable Google Tag Manager in Shopify

I get lots of questions on how to add events in Shopify. It is tough to do that without using Google Tag Manager(GTM). However, there is no space in Shopify preferences to add the GTM code. So one is stymied.

Not quite. There is a sneakier way to still do this. So let’s give it a shot.

Step 1

First, add the UA-XXXXXXXX code that you get from Google Analytics into the box marked in Shopify as seen in the picture below.

 To get to this page, you need to click on “Online Store” on the left Menu and then click “Preferences”.

Step 2

Now, go and build a GTM container. An excellent video to show you how.

Now that you are done deploying Google Tag Manager, put a couple of events together in the container besides page view tracking.

Step 3

Let’s choose scroll tracking and outbound click tracking because they can apply to pretty much every website.

For Scroll Tracking, again an excellent video by LovesData will guide you.

Now, try and put together an event for tracking outbound links. We will use another great video by Whole Whale on YouTube.

Step 4

Now that you have configured both events, go ahead and pause the tag for page view tracking. Let me show you how to do that.

Step 5

Now that all these steps are done, copy the code for the header from GTM. It will look like this

<!– Google Tag Manager –>


new Date().getTime(),event:’gtm.js’});var f=d.getElementsByTagName(s)[0],



<!– End Google Tag Manager –>

Step 6

Let’s go back to the Shopify interface and add the GTM code above into the yellow area shown in the image below

Click Save in the top right corner(not shown in the picture).

This is going to make sure that all the events that are added can be fired through Google Tag Manager. The page view collection will be done by the Shopify integrated Google Analytics code. In case you are not able to add the GTM code and get an error that says, “Analytics extras is disabled for the moment. Contact support to enable editing of this field”, you do not have admin permissions. Either get them assigned to you or ask the admin to put in this code for you.

Step 7

There is just one more thing, add a product in your shopping cart and go to the “/cart” page. You will notice that GTM does not show up at all. This is a bit of a doozy and has to do with the interaction between GTM and Shopify. To get over this, you will have to take the footer code in GTM, given below:

<!– Google Tag Manager (noscript) –>

<noscript><iframe src=”;

height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>

<!– End Google Tag Manager (noscript) –>

..and add it to the start of the <body> tag in theme. liquid file. You can place it at the end of the body tag too. This will enable GTM code on the checkout page. Please save the theme.liquid file and please re-load the website to make sure nothing is broken. Of necessity, you are doing this in production( small shops don’t have a staging Shopify environment), so it pays to be extra careful.

What happens if the site breaks?

Don’t panic! Just revert to the earlier theme.liquid file and make sure that your copy-paste job has the correct code. Re-input the code within the body tag; save again and check again. If it doesn’t work again, revert to previously working code and notify your programmer. They will help you fix this issue.

This is a hack and you should not assume that it will work in your account. Please test and make sure that only one pageview tracker is firing. A surefire way to know that two trackers are firing is to observe bounce rates in Google Analytics. They will immediately go to zero.

I hope this works for you. Do let me know if you have any questions.


Indian Elections and Google Trends

Google Searches for Mr. Modi

Mr. Banerjee at very interestingly put together a post saying the Google Trends predicts another government by Mr. Modi for the next five years. The elegant chart below tries to reproduce from Google Trends exactly his findings.

Last 90 days party comparisons

We are looking at data for the last 90 days in India. As you can see, it’s all BJP! So is he right?

Well, it depends….

You see, the way that Google Trends displays data is by an index of the most popular of the terms being compared. In this case, BJP is undeniably more popular. However, if we see the search for each party individually and try to figure out who is increasingly more popular( only on Google though), these are the pictures that emerge.


Indian National Congress

Although the charts show that BJP’s popularity is on a sharper decline, both parties’ popularity is on the wane, with BJP holding more sway in UP, Odisha and West Bengal. For the INC index, it shows higher values in Rajasthan, Madhya Pradesh and the North Eastern states.

These are indexed numbers so the fact that BJP is searched for more is still not challenged. It’s just the luster seems to be going off a bit in the last few months. The Prime Minister though, asks for votes these days in his name, rather than the BJP. Let’s compare the two search terms.

Well, well! What do you know! It seems that the data people for the BJP have been urging him to take that line because while the BJP’s popularity is on the downswing, Mr. Modi is riding high.

However, this is still an index of search terms, not actual values. Perhaps, we should try and compare the popularity of the Prime Minister for a longer term and see how that has changed over time.

So, we go back 6 years and see where things were then for Mr. Modi.

As you can see, this search term is about half as popular in India as it was in May-2014. It seems to be showing a rising trend now but would it go to the same level as that resounding victory? It’s anybody’s guess right now.

Considering that the BJP is the incumbent on 72 out of the 80 seats in Uttar Pradesh, how does the Prime Minister find himself in the top districts there?

Nowhere near his peak except for a couple of districts.

So what does this all mean?

Looking at the data from various perspectives adds to our understanding of it and perhaps muddies a clear-cut decision. In this case, perhaps all we can say is that this specific dataset does not pre-sage a clear victory for anyone. Particularly, if we consider the fact that there is no measure of sentiment involved here. Look at this picture of Google Suggest and you will see what I mean.

Google thinks the searcher(I am incognito) is more interested in his wife, caste and education. So dear reader, that’s why economists are prone to always say “It depends”.

Analytics, Business Intelligence

A Paradigm for Business Intelligence Evolution


This is a series of ideas that will outline how we need to build an e-commerce Analytics solution for a digital marketing agency or consultant to join the data from all channels and client’s data together in one central repository. This data should include online transactional data as well social interactions.

This repository should also be able to import offline customer information too. Additionally, this data will then be combined to create analyses across platforms and, ultimately to be used as a source of predictive analysis.

We will end this document with some timelines around the tools to use, the timelines around their deployment and kind of internal team to execute this plan.


Process Flow in Summary

  1. Setting up data collection within current data sources.
  2. Merging all data sources into one platform and automate such a collection.
  3. Analyzing patterns in these datasets to builds reports and dashboards based on KPIs
  4. Based on past behavior of customers, create prescriptive and predictive Analytics around key metrics and goals.


Data Collection

It is imperative to get all the measurements set up correctly in every marketing tool that is used. We tend to focus mostly on Google Analytics and believe that data alone. Sometimes, when tagging is incorrect on email platforms, Facebook or Bing, we cannot even measure conversions in the same way and that leads to reporting that is erroneous and leads to bad decisions. In this step, it is important to measure each source of traffic to a web domain and be able to capture that either within that domain (for example: clicks from Facebook reporting) or make it work so that the data is captured correctly in another tool like Google Analytics or Adobe Analytics. We need to be able to account for 90% of traffic correctly before we move ahead.

As this data collections are all in separate silos that usually do not support each other well, we will get some numbers that will be off between each tool. That is a cost of doing business at this stage and we should only focus the primary data source when making decisions. For example, if we want to determine whether Facebook CPC is effective, we use the conversion metrics from Facebook reporting rather than try and match that data with Google Analytics.

This becomes rather onerous and requires us to look at several disparate places to get a complete picture of our business, so we then are forced, for the sake of efficiency to move to the next stage.

Merging Data Sources

At some point the enterprise slows down because there is too much data, but no unit can understand the other’s language. If there is an Analytics team, an SEO and SEM team, the confusion arising from a lack of a “single source of truth” can be quite debilitating in terms of decision making. At this point we start to go down two different pathways. We can either build our own data warehouse or buy a tool that will do this for us.

Building a data warehouse comes with the advantage of designing your own data collection and downstream KPI’s that the can be customized as desired. There is a high initial cost but then the flexibility of operation tends to result in increased ROI. Dealing with internal resources also means a quick deployment of any changes and newer traffic sources can be ingested sooner. The data warehouse can be used with any of the data visualization tools like Tableau, Power BI, Qlik or Google Data Studio.

Buying a data merging tool like, Alteryx, Looker, ReportGarden, Optymyzer or BIME has it’s advantages in that the initial cost is lower and they provide built-in connectors to all major data sources. You can also customize them by importing new data sources using API’s or just csv files. Over time, they also end up creating new features and introducing concepts of machine learning and programmatically produced insights that can be of great use to clients also. One doesn’t have the flexibility of an in-house team though.

For an agency, perhaps the latter option is one to start with and the former to aim for in the long-term if the revenues from the Analytics stream are high enough.



Once the processes are in place and perhaps even at the point of data collection, we need to have a core set of people who are going to make sense of all the analysis and provide recommendations to the clients based on all the dashboards and reports that can be built. It has been my experience that lots of dashboards are made and reports are regularly generated but the consumption on the client side is poor because the analytics practice does not translate the numbers into business speak. Please read this great article by Avinash Kaushik that further elaborates the point.

I cannot stress enough the fact that the quality of good analysis and a set of concrete recommendations that clearly explains what the client is going to get by following them goes a long way in the adoption of data driven decision making. For example, a recommendation that says “Moving the add to cart button 300 pixels up will increase revenues by $30k a month” is much better than saying “We find that testing suggests moving the button 300 pixels up will increase conversion rates”

A good web analyst will have experience working with product development, a smattering of coding and long exposure to different kinds visualization techniques. An investment in a good resource(s) of this kind will yield more value over time than the best tools around.


Predictive Analytics

Once a website is running and customers are using it, a one size model in terms of pricing, deals or discounts does remain a good strategy for long. Very soon clients run into a wall where they keep acquiring new customers or current customers without enough understanding of any of them. At this point, you are looking for tools that segment the population in meaningful chunk and target them based on their needs at a particular time. A pet food company can predict when a customer will run out of the current cat food that they have purchased and send a targeted message (email or ad or website chat) at the right time.

There are several open source languages like R or statistical packages like SAS and Tableau that can help an internal analytics team do this work for a client. These require the in-house team to have a strong data science background to create a product offering for clients. This method will give you the best ROI because the tools will be made in-house and can be customized based on a client.

The other method is to buy a tool like Domo, Qlik, clearbit, Zaius and use it’s features to conduct user based analyses that can segment and target customers in different ways.


Timelines for Capability Development

NOW: We are doing Data collection and collation right now in our efforts to serve customers. Pulling data for separate business functions from different tools is the ground situation now.


Six Months: We can build up a capability using an external tool to combine data sources and provide comprehensive and cross-platform reporting.


18 Months: It will take a lot of time and learning to build up an analytics resource team in-house that has experience in all the different domains. If you start hiring now, such a team will be able to start adding value within six months and be giving very good ROI in a year and a half.


2 Years: This is the time it will take for a good team and a set of tools to be able to make deep inroads and to build predictive analytics and modeling capabilities. It will also take time for our client base to become mature enough to realize that this is the ultimate need for any online business.



I hope this plan gives you a sense of what I think the plan to success in the e-commerce Analytics domain. There are many ways besides this to get data together and put analytics to work but in the e-commerce world this should work the best.




Google Tag Manager

GDPR Compliance for Google Tag Manager and Google Analytics

The time is upon us when we all have to comply with the EU’s regulations on privacy. While websites in the USA don’t have to technically comply BUT everyone accessing your site from the EU is protected under this statute and so it is best for all of us to “get with the ball game”, so to speak.

So what are we supposed to do? Let me list down the steps that need to be taken.

Step 1: Data storage in Google Analytics. Please go into the Property Setting and look for the Data Retention menu as below:

Make Data Retention as long as possible

Click on the tab and you will see choices of how many months of data to keep. I went for 50 months because that is the maximum time explicitly allowed. I wouldn’t recommend “Do not automatically expire” option because it is quite vague.

The option “26 months” is a Google-suggested default, just so you know. Here is some more information on Retention.

Step 2: Anonymize IP. If you use Google Tag Manager as any normal human being should, this is a very simple setting change. Please go into the Universal Analytics tag that fires on every page view and set the Fields to Set to have anonymizeip as TRUE.


Now, the last 4 digits of a user’s IP will be marked as ‘0000’ and you cannot identify them in any way. Not that you were doing anything with it but still, that is the guideline.

If you are not able to use GTM for any reason, just change the normative collection by adding the following to every page of your site.

<!– Global site tag (gtag.js) – Google Analytics –>

Step 3: Advertising and Re-targeting. Some of the more conservative folks are recommending that you shut down the re-targeting and advertising features in Google Analytics. If you are not using them, do turn them off, otherwise there is no harm in keeping them on.


Step 4: Set up a cookie consent form. The best recipe that I have seen is by our friends at AnalyticsMania. This is a great a quick recipe to install using Google Tag Manager. It will put you in more compliance than is absolutely necessary.

One more thing…..

Step 5: Audit your site so that there is no PII being sent to Google Analytics. So any emails, userID’s or any unencrypted content. I know a lot of folks who have email addresses floating around in URL’s or events.

Please desist!

You do not want to be banned from Google. Getting back in requires and lot of work. Clean all new data collection to make sure nothing personal gets recorded in Google Analytics. Read this article; memorize if necessary.

You are all done and now can rest easy that the EU will not come after you.

Analytics, Google Tag Manager

UserID tracking in Google Tag Manager(GTM)


Why do we need to do that?

Ostensibly Google says that you do this for tracking true users of your site. Since multiple devices generate a visitor cookie each, they get counted as different visitors. You can stitch sessions cross-device by creating a userid view.

However, if we want to track CAC and LTV by referral sources and we have only Google Analytics and an internal database around, we can piggyback on this method to measure them.

It is tricky and will require some testing before it runs in your environment. However, it works and I have managed to figure it out in Google Tag Manager and will lay it out for you dear analyst, step by step.

Step 1

Create the User-ID view in Google Analytics. Go to the Property and expand “.js tracking Info”. Then follow the prompts as seen in the picture below.


After you have done the above, let’s create a dimension to store this data.

Step 2

In the same property column, below the PRODUCT LINKING tab, sits a place to create custom dimensions as shown in this picture below.



Click on create dimension and add the custom dimension as shown below:


Please note that you have to name it userID. This is because I am going to name the data layer variable later the same. They have to be so to capture your user information.

Step 3

Have your awesome developer pass the user-id value to the data layer variable called userID. The values that it should spit out if you click “view-source” on your site should look like this:

dataLayer = [{‘userID’: ‘yyyyyy’}];

dataLayer.push( {‘event’:’uidAvailable’, ‘uid’:’yyyyyy’} );

They can push this change live anytime and will be quite thankful for the Starbucks drink you will treat them to.

Make very sure that this userID being made cannot be Personally identifiable information(PII). So no names, email addresses, etc.

Step 4

Ok! Let’s push on now to the Google Tag Manager and the last part of this implementation.

Open up your tag manager and on the left hand side where it says, variables, click and add a user defined variable. We will call it userID.


Now, on the left hand side open up the tags section. On all the tags that have anything to do with an event or page view, we will make slight edits. Let’s start with the Google Analytics tag (it should be set to universal Analytics otherwise this will not work).


We add two variable to be filled when the tag fires, one is the userId (please note that this is the exact name you need to set). The other is the custom dimension that we have created in Google Analytics so we can run reports based on our own user-ids, which I am sure, are aligned to email addresses.

Step 5

Now, let’s set our workspace on Google Tag Manager to Preview the changes that we have made. If this works, the Real-Time reports in Google Analytics will immediately start collecting data. Once it works, then go ahead and publish the new tags.

Now, you should be free to download the userID and referral source. You can download the LTV and userID from your own database and analyze in Excel or any other spreadsheet of your choice.

You would have noticed that I keep harping on the name and case of the variable “userID” and “userId”. I thought to name all variables the same and it didn’t work. However, hard-coding the variables works perfectly!

Do let me know if you have any issues with this and I can walk you through the implementation, if need be.