Advanced Google Analytics for Startups

Growing a business is an endless cycle of experiments. You get an idea about why some part of your business is going the way it is, you tweak things a bit to test your idea, and then you review the results. For a web based business like Think Vitamin Membership, Google Analytics is an awesome way to collect information while experimenting and study what’s going on with your web app.

For the longest time I thought that Google Analytics was something that you just add to your site and review from time to time. I’ve learned, though, that tweaking Analytics can give some terrific data that wouldn’t be available by using their default setup. This week we implemented some new tweaks that we’ve made to our analytics setup that might be useful for others, so we figured we’d share them.

Custom Variables and Advanced Segments

One of the biggest questions that we weren’t able to answer with Analytics before this week was what the traffic breakdown between members and non-members was. You can look at new visits and assume that they’re non-members, but that’s a pretty big assumption. You can also assume that members only pages are only members, but that still leaves a lot of traffic up to interpretation. To solve the problem of understanding the split between members and non-members we’re employing Custom Variables.

A Custom Variable in Analytics lets you tag either a page view, a session, or a visitor as having a certain attribute. Since we were interested in noting whether or not a user has ever logged in, we went with a visitor variable. Here’s the JavaScript code for setting a custom variable:

1
_gaq.push(['_setCustomVar', slot, name, value, scope]);

Google’s JavaScript can look pretty strange, but you can basically think of the _gaq.push method as pushing commands to Google. It takes an array, and the first element in that array is always the name of the command to send. The following elements in the array are the arguments for that command. The arguments for the _setCustomVar are:

  • slot – An integer between 1 and 5. The purpose for the slot number will make more sense when we look at analyzing custom variable data.
  • name – A string containing the name for the custom variable
  • value – A string containing the value for the custom variable
  • scope – An integer for scope for the custom variable.

The scope can have one of the following values:

  • 1 – Visitor scope. This variable lasts as long as the visitor’s cookie is present
  • 2 – Session scope. This variable lasts as long as the current visit lasts in analytics
  • 3 – Page scope. This variable is only in effect for the current pageview

In our case, we used the following code:

1
_gaq.push(['_setCustomVar', 1, 'Member?', 'Yes', 1]);

This code sets a new custom visitor variable named “Member?” with a value of “Yes” into slot 1. In our page we wrapped this code in a conditional so that it only appears in each page if a visitor is logged in.

Initially we also included code that set the variable to “No” if a visitor wasn’t logged in, but realized that if a user logged out the value for the variable would be changed from “Yes” to “No”, so we wouldn’t remember that the user actually was a member. In our case we’d rather remember that a visitor was a member even if they log out, so we got rid of that bit of code that set the variable to “No” when a user wasn’t logged in.

So we’ve got our variable set up. What do we do with it now? That’s where Advanced Segments come into play. To set up an Advanced Segment, log in to Google Analytics and navigate to the profile that you’ve added your custom variable to. In the sidebar, under My Customizations, click on the Advanced Segments link to get to the Manage Advanced Segments page. Once you’re there, click on “Create new custom segment” to build your advanced segment. The tool Google created to design advanced segments is pretty neat – it lets you drag metrics from the list on the left to build the logic for your segment. Here’s what my segment for Members looks like:

Notice how I’m using the “Custom Variable (Key 1)” metric. That maps to the first variable slot that we used when we set the variable. Here’s my segment for non-members, which just checks that slot 1 isn’t set to the “Member?” variable:

Now that we have our segments set up, we can select one or more of them from the segment dropdown on the top right side of almost every page in Analytics to focus on those segments.

Events

One other problem I noticed in Analytics was that we could see when a user comes to one of the pages for our videos, but just visiting the video page doesn’t necessarily tell us if the user watched the video or not. Google Analytics Events allow you to track arbitrary events with JavaScript. Our player includes JavaScript callbacks, so using Events was a perfect fit.

The Google Analytics JavaScript for tracking an event looks like this:

1
_gaq.push(['_trackEvent', category, action, label, value]);
  • category – A string representing the category of the event
  • action – A string representing the type of interaction that occurred
  • label – (Optional) A string representing an additional layer of detail about the action
  • value – (Optional) A integer value relating to the action

Here’s the JavaScript code that I added to our player’s callback when playback is started:

1
_gaq.push(['_trackEvent', 'Video', 'Begin', videoName]);

You can review the results of your events in Analytics by going to Content, Event Tracking in the sidebar. The views for Events let you drill down into each event action to see the list of labels and values for that action. In our case that means that we can drill into the “Begin” event to see exactly what videos were watched and how many times they were watched.

Search Analytics

One last feature of our site that I was interested in getting more information about was search. We allow users to search for videos, but we haven’t been tracking those searches at all. Setting up search tracking in Analytics usually doesn’t require any changes to your site’s code. All you have to do is go to the settings for your site’s profile in Analytics, enable Site Search, and enter the query parameters that you use for search terms on your search forms. On our site we always represent a search term with a query parameter named q.

In the Analytics dashboard you can go to Content, Site Search to review your site’s search data. Google includes information on how many searches were performed, what terms were searched for, and what pages users went to as a result of the search.

Finishing Up

Well, now that we’ve tuned our Google Analytics settings we’ve got to wait a bit and then we’ll start making tweaks to our app as a result. I’m hoping that segmenting our members and non-members will tell us more about what topics non-members are interested in and also help us figure out the best ways to get users to our sign up page. The video events and search statistics should give us interesting information about how we’ll we’re helping people find the videos they want to watch. Once we’ve gathered more data and made some changes, we’ll probably circle back and fine tune what data we’re collecting with even more custom variables and events. It really is a never ending cycle.

Do you have any tricks that you’re using with Google Analytics? We’d love to hear about them in the comments!

Free Workshops

Watch one of our expert, full-length teaching videos. Choose from either HTML, CSS or Wordpress.

Start learning

Treehouse

Our mission is to bring affordable Technology education to people everywhere, in order to help them achieve their dreams and change the world.

Comments

36 comments on “Advanced Google Analytics for Startups

  1. This was really helpfully, thanks!

    I have been wanting to get my head around Custom Variables for a while. 

    A new toy for tomorrow :-)

  2. Some really simple, but great, advice. Customizing analytics can be remarkably helpful, especially when it comes to multivariate testing.

  3. Thanks very much, I’ve always gazed over this stuff in GA because I don’t want to spend the time learning it – appreciate the post.

  4. I’ve only been using custom variables for a week or two, has anyone else noticed a longer lag than usual before they show up in your analytics?

    • @eurica:disqus - I noticed that it took about a day for them to show up, but nothing like a week or two. I set up most of the custom variables in this post on Monday and was able to see results for them on Tuesday afternoon.

    • I ran into a similar situation in the past, just make sure you’re using the latest asynchronous tracking from Google.

  5. As a small business owner, it’s extremely important to understand how different visitors from different sites perform. For example, GA gives you a few simple variables when you click ‘traffic sources’ but you can’t really go in depth from there.

    To solve this, we’ve created custom segments for our 3 top traffic sources (Yelp, Google, Citysearch) and now we can easily filter any page in GA and see how ourdifferent sources perform.

    I much prefer this to using the clumsy table interface provided by default in Traffic Sources.

  6. I don’t have any on Google Analytics, but Hoot Suite is a great analytics tool for social media.  http://www.hootsuite.com

  7. think the 1st example/prototype under ‘Events’ is missing the closing array bracket ‘]’ (FYI)

    Thanks! Nice article – been meaning to read up more on this – didn’t know how easy it was. This article helped further my understanding.

  8. Alan – Your post got me thinking. I always wanted to see if people download a certain pdf from a page that they visit, however, Google Analytics doesn’t track that. I’m not a coder, so I’m wondering if there is a script, similar to the javascript you wrote to be able to track that? Or if you have any resources you could point me to. 

    Thanks for the informative post!

    • You can use event tracking with the onclick event to catch all of the downloads that come through a particular page on your site. Your onclick event would need to look something like the example I just typed up at https://gist.github.com/993699.

      • Wow, you’re quick! I’ll implement this and see how it goes. Thanks so much, looking forward to more of your posts

  9. wow, very informative. I’m a newbie in google analytics and I only look at the worldwide map and smile, don’t know why. But I will apply this tips on my account for sure.  Thanks, Alan!

  10. Events is pretty good at tracking form abandonment as well. You can use jquery to record an event on the input blur. If you see that the events drop significantly after an input you may have to redesign your forms.

  11. Just one small tweak to avoid an the extra conditional statement. When you set a visitor level variable you don’t need to set it more than once since it persist across the life of the visitor cookie. 

    So you could set it once in the signup-process, or maybe in a log-in action.

  12. “help us figure out the best ways to get users to our sign up page.” – lower the price of membership :)

    but on a serious note, great article. i too was in the same ship as you with “analytics just kinda is there.” after reading this article it has opened me up to some really cool ideas.

    • If you have feedback about our pricing, though, we’d love to hear it – email us at help at thinkvitamin.com.

      I’m glad you’re looking a bit more at analytics. I’d love to hear what you discover from taking a closer look at your site.

  13. What happens if a user visits the site for the first time, registers, then logs in. The visit/visitor will be regarded as a member. This pollutes the data, no? 
    Say you wanted to find out what your sign-up conversion rate is for unregistered users. How would you measure this? If registered users also log in from the front page of the site (which is a common setup), I can’t see how you could track sign-up conversion data?

    • In that case you’d certainly need to re-engineer things a good bit. At that point I’d probably either use the page that the signup came through (was the user logged in or not), tweak the URL that the signup finishes through, or just set the variable immediately after the user logs in (or gets automatically logged in if you have a remember me feature) so that it only gets set for users who log in (but not for those who sign up).

  14. Nice informative article, it’s good to see others tweaking out of the box. I’ve used event handles to track media usage on a content heavy site, it worked like a charm!

  15. One drawback of Event tracking is that you currently can’t use them as part of your Goals and Funnels. One way to get around this is to use Virtual Pageviews (i.e. fake pageviews for events such as button clicks, PDF downloads, form submissions, etc.)

    Google has said they will be incorporating Events in Goals in the new version of GA, but in the meantime I can highly recommend Virtual Pageviews as a work-around for tracking conversions and user behavior on your site. 

    More info here: http://code.google.com/apis/analytics/docs/tracking/asyncMigrationExamples.html#VirtualPageviews

    • Thanks for the tip, Hessam. In our case we weren’t looking for a playback event as part of a goal.

      Overall, though, this brings up something I’ve wanted to see in Analytics, which is kind of rough view for conversions that lets you see what’s happened in the past for them. Something like 55% of conversions watched a video. 33% went to a certain URL. 25% were referred by a specific site. Things like that.

  16. This is a great thing to know about it. Thanks. I should point out though, that I just tried it out and discovered that the setCustomVar line should be before this line of the standard GA code:  
     _gaq.push(['_trackPageview']);
    At first i was putting it at the end of the js block and the variable wasn’t showing up.