How to fix ReferenceError: datalayer is not defined in Google Tag Manager

I was using Google Tag Manager and custom event tracking to track time on site and was noticing a console error ReferenceError: datalayer is not defined – after investigating I found a good summary on how to use the data layer from Lunametrics.

Here is how I changed the Custom HTML tag to resolve the error.


window.addEventListener("beforeunload", function(e) {
window.dataLayer = window.dataLayer || [];
datalayer.push ({
'event' : 'beforeunload',
'timeOnPage' : new Date().getTime() - {{Page Load Start Time}}
});
});

 

I changed the Custom HTML Tag and this resolved the console error. Notice where I instantiate the dataLayer before pushing the event and timeOnPage.


window.addEventListener("beforeunload", function(e) {
var dataLayer = window.dataLayer = window.dataLayer || [];
dataLayer.push ({
'event' : 'beforeunload',
'timeOnPage' : new Date().getTime() - {{Page Load Start Time}}
});
});

 

Shopify and Google Analytics: 5 Tips You Need Before Pulling Your Next Report

The most successful Shopify store owners use data to inform their long term decision making process – and validate success or failure. I get it – Google Analytics and eCommerce reporting can be overwhelming. What’s more overwhelming is that If you aren’t diving in to Google Analytics at least once a month – you’re missing out on revenue opportunities.

Whenever I look at a client’s analytics account – I want to understand (and share with them):

  • What’s working (Success)?
  • What’s not working (Failure)?
  • What haven’t we done yet (Opportunities)?

So take deep breath and use these 5 tips to use Google Analytics and Shopify to make decisions that improve your Shopify revenue. Celebrate your success, learn from your failures, and keep taking on new opportunities!

Shopify Tip #1: Filter out Owner, Staff, and Vendor Traffic

Why? If you put good data in – you MAY get good data out. If it’s bad data to begin with… well – that’s why it’s number 1 on this list. As you move down this list, with your own browsing activity, behavior, and in all honesty lack of purchases excluded – everything becomes more clear.

How: Set up a separate view in Google Analytics and add a Custom filter that Exclude with a filter field of IP Address and enter your IP. Repeat this process as much as necessary – and use this view that shows your customer’s behavior to run reports and track progress.

Exclude IP for Shopify Store in Google Analytics

To collect this information from staff or partners have them visit a website like WhatismyIPAddress.com and have them send that to you to add additional filters.

Shopify Tip #2: Track Shopify Conversions in a Google Analytics Funnel

Why? A Shopify Conversion Funnel in Google Analytics provides insight into where the customer abandons checkout. We need to obsess about minimizing that abandonment. Yes – Google Analytics has eCommerce tracking out of the box and it works. I recommend setting up a Conversion Goal to see the funnel visualization of a customer experience  so you can better identify the drop off.

I used this report with a client to identify that mobile customers were abandoning the first step in the checkout process. I compared the mobile UI to the desktop UI and noticed the discount code input field was less visible on a mobile device. I took action by adding the coupon code to a different position in the mobile view which improved the mobile conversion percentage generating more money for the Shopify store owner.

If you want to add a Shopify Conversion Funnel to your Analytics View – click the link below to import it from a shared gallery. It’s fast, free, and comes from Google Gallery. Do It!

Click Here to Add Shopify Conversion Funnel To Your Analytics View

Shopify Tip #3: Track and Monitor Mobile and Desktop Conversion Rates

Why? Conversion rate is way more than the number of unique conversions compared to number of visits. Compare your mobile conversion rate to desktop and investigate what you could do to improve both.

An update made based on a review of the mobile conversion rate was better presenting the Add to Cart button on a product page to avoid lengthy scrolling. If I simply looked at the conversion rate as a whole – we might not have seen the lower conversion rate on mobile and taken action.

In addition to the conversion percentage, combine this device specific report with the source that contributes to conversions to understand what’s working.

Now that you have a conversion funnel and you are filtering out non-customer traffic – we’ll have a better insight into what’s working.

To pull this report go to Conversions > Goals > Overview > Source / Medium

Google Analytics Shopify Conversion By Source

This report helps your bottom line by informing you where your best customers come from and where to invest in advertising or content production.

Shopify Tip #4: Exclude Self Referrals and More

Why? Nothing is worse than celebrating a great month – pulling your reports and seeing that conversions were attributed to paypal.com or checkout.shopify.com – negatively impacting your reporting and understanding of what is working.

Here is what you do – In Google Analytics Open the Property that has filters to exclude staff and Vendors and select Referral Exclusion List  > Add Referral Exclusion and add the following

  • yourwebsite.com or other variations of your website if it redirects
  • checkout.shopify.com
  • paypal.com

Shopify Referral Exclusion List

If you aren’t excluding domains from referral traffic in Google Analytics – they are going to have sales attributed to them when you pull your reports. If you see any external payment providers or odd domains – consider excluding those.

Shopify Tip #5: Track Exits and Bounce Rates on Collection Pages

Why? If customers are not making it to your product page it doesn’t matter how much you are optimizing the product detail page.

Bounces are single page sessions where the user browsers to the website and does not visit another page. The first page in a visit is an opportunity to start the customer journey. Exits are tracked and associated to a specific page when that is the last page the user visited in the session. A page with a high exit rate indicates that users are not engaged, possibly confused, and looking elsewhere.

I use Google Tag Manager to pull the following standard report for collection pages:

  • % of collection visits where a user then clicks on a product to view more specific information
  • % of visits to a collection page that complete a conversion
  • % of exits per collection
  • % of bounces per collection

When you set up these reports remember to filter activity so you only see collection pages – and not products!

Shopify Collection Google Analytics Report Filters

Track Shopify Search Terms Using Google Analytics

You can learn a lot about your users when you track shopify search terms using Google Analytics. You can better inform your site navigation and segment the searches by device or term to see if you aren’t providing information to users in certain contexts.

Your website’s navigation is important and while lots of customers will browse the navigation your site must cater to users that will navigate by using your site search.

Why Track Search Terms in Google Analytics?

Yes – Shopify offers search stats in your Dashboard. The advantage of tracking search terms in Google Analytics is that you can parse the data by segments.

What if you saw that a significantly higher percentage of mobile users were searching for the term “returns” as compared to desktop visits.

You can use that data to evaluate how easy it is for visitors to find your return policy on a mobile device. Maybe you hide that at a mobile break point..

How about if you see International visitors are searching about your shipping policy. You can use Geo-Location to show/hide content more related to international shipping rates/time. (Post Coming Soon on This)

How To Track Shopify Search Terms in Google Analytics?

Settings to Enable Site Search Tracking in Google AnalyticsOn your site search for a product or phrase. In the URL you’ll see something like search?type=product&q=clasp. With that information you can log in to your Google Analytics account – select a View that you use to see customer traffic – and open the View Settings and scroll until you see Site Search Settings. Turn this ON and then enter in the Query Parameter field q and save.

Where to find Shopify Search Terms in Google Analytics?

This is a screenshot of where to find Shopify Search Terms in Google AnalyticsWithin 24 hours you can then go to that same Analytics View and navigate to Behavior > Site Search > Search Terms to start to investigate the search terms to find patterns to improve your customer experience.

 

If you need help tracking data about your visitors – Contact Me

 

 

 

 

 

 

Track Active Time On Page Using Google Tag Manager

Track Average Engagement time using Google Tag Manager to better understand active time a user is on a page or website. These days customers may find your site or product and keep a tab open for hours in between other tasks while spending.

This visit is recorded in Google Analytics and the time where the user is inactive may inflate the Google Analytics metric Average Time on Page.

Even worse – If your user finds a detailed blog posts and reads it ALL DAY (like I sometimes do) and then close the browser (and not visiting another page) – they are marked as a bounce and don’t contribute to Average Time on Page because they never visited another page.

Seriously – Google Analytics defines a bounce as “A bounce is a single-page session on your site”

When you bounce – the time you spend on that single page isn’t sent to Google Analytics. That’s a problem I’ve been trying to address.

I was able to successfully capture “EngagedTime” on site based on the instructions in the two posts below:

Part 1: https://www.simoahava.com/analytics/track-content-engagement-via-gtm/
Part 2: https://www.simoahava.com/analytics/track-content-engagement-part-2/

I stopped at step 4 in Part 1 so that I can track without using User Timings in Google Analytics. User Timings has a limit of 10,000 sessions/day.

Part 2 of the instructions tracked active time on page using a Custom Metric, measuring engagement time in seconds.

With Tag Manager, we track the length of time a user is active or engaged based on the following behaviors:

1. Mousedown
2. Keydown
3. Scroll
4. Mousemove

If we see more than 5 seconds where they haven’t completed one of these behaviors – we indicate they are “idle” and do not track that time as engaged.

Every 15 seconds in Tag Manager an event is fired that passed the  cumulative “engaged” time on site. With this solution we can track active time on page to better understand the amount of time a user is focused on your content, and cut down on inflated Average Time on Page that can occur when a user is inactive.

If you need help tracking data about your visitors – Contact Me

Track Audio Plays, Pauses, Completions using Google Analytics

If you have a website or page that embeds multiple podcasts or audio files on a single page – you’ll want to Track Audio Using Google Analytics like plays, pauses, and completions.

Background: 

I encountered a request to track the number of audio plays on a website. I’d prefer to track this using Google Tag Manager, but the website was built by a third party with only universal analytics so I needed a Javascript solution since the effort to add the Tag Manager container wasn’t going to meet the go live deadline.

Here was some code I found and tweaked to be compatible to track events in Google Analytics on audio files when a user plays, pauses, completes listening to an audio file.

Solution: 

$("audio").bind("play", function(){
ga('send', 'event', 'Audio', "Play", $(this).attr('src'));
});


$("audio").bind("ended", function(){
ga('send', 'event', 'Audio', "Ended", $(this).attr('src'));
});


$("audio").bind("pause", function(){
ga('send', 'event', 'Audio', "Paused", $(this).attr('src'));
});


$("audio").bind("seeked", function(){
ga('send', 'event', 'Audio Seek', "$currentTime", $(this).attr('src'));
});


$("audio a").click(function() {
ga('send', 'event', 'Audio', "Download", $(this).attr('src'));
});

If you need help tracking data about your visitors – Contact Me