Find Out Where Your Leanpub Book Sales Are Coming From (a.k.a. Setting Up Conversion Tracking in Google Analytics With Regular Expressions)
published Aug 14, 2014
(Please note that some of the information in this post is out of date. There’s a newer post with a few details here.)
Guest Post Author Bio
Visnja Zeljeznjak co-founded a web agency in 2002 and distilled her experience into her first book Recurring Revenue For Web Agencies, published with Leanpub. Visnja writes articles about running a web development business at Simpfinity.com, her company’s upcoming productivity software for web development agencies.
If you’re publishing your book with Leanpub, you’re probably responsible for your own book marketing and sales too. For an online entrepreneur — and book authors are entrepreneurs — there is one tool that makes the biggest difference between having a chance online and not having it. Google Analytics is that tool.
The best news is that Leanpub is already integrated with it, but there is still some manual work you need to do in Google Analytics to complete the process of tracking book sales. This tutorial will teach you how to set up the so-called conversion tracking goals by using something we call regular expressions.
Regular expressions are currently the only way to track Leanpub book sales. If you’ve never written one, they do seem scary. Fortunately, even inexperienced and non-technical book authors are capable of using regular expressions to set up book sales tracking. This tutorial’s main goal is to remove the scary part of that task.
How this article is organized (table of contents)
Skip directly to the chapter that interests you the most:
Why track book sales with Google Analytics
Google Analytics can help you figure out where people who bought your book are coming from. Hundreds of reports in Google Analytics can show you exactly which marketing efforts of yours are working spectacularly and which are a complete waste of time. For example, Google Analytics can help you measure:
- whether you’re wasting your time on Twitter or not (see how this Leanpub author used Google Analytics to answer this very question)
- whether or not you’re getting buyers from organic Google searches
- the exact percentage of people who read your email newsletter and subsequently bought your book
- whether or not you’re getting return on your paid advertising campaigns on Facebook, Google AdWords or Reddit
What you need before you start (prerequisites)
Before I show you how to set up goals with a regular expression, let’s first make sure that you’ve completed three necessary steps.
Throughout the tutorial, I will be using screenshots from my own Google Analytics account and my own author interface on Leanpub.
Prerequisite #1: you have already set up a Google Analytics account.
If you don’t have one, read Google’s official instructions on how to create a Google Analytics account. It’s easy to setup with any email address you own.
We will assume that you’ll be tracking your book sales with the same Google Analytics account that you’re now using to track your website visitors. You don’t need a website to be able to track book sales on Leanpub, but most authors do have it, and that will be my default assumption in this tutorial.
Prerequisite #2: you have already entered your unique Google Analytics tracking ID (something that looks like this: UA-12345678–1) into the Leanpub admin.
If you haven’t done this yet, here’s how and where you do it.
- First we need to locate the UA tracking ID in Google Analytics. Log in to Google Analytics at https://www.google.com/analytics/ and click
- You will see something like this:
- In the left-most column
Account, make sure that you have selected the proper account from the drop-down menu. This is important to people who are tracking multiple websites under the same Google Analytics account because they will have multiple entries here. Select the one that you will be using for tracking your book sales. If you have only one website in your Google Analytics account, it will be listed here as the only option.
- In the middle column
Tracking Infoand then click
- The Google Analytics interface will show you a big bold tracking ID (in my example, the tracking ID for my website is
UA-46516970-1, yours will be similar). Select that code with your mouse in your browser in its entirety, copy it (CTRL-C / CMD-C) and paste it in your favorite text editor, i.e. Notepad or TextEdit for later use.
- That’s everything we need to do in Google Analytics for now. Let’s now switch to Leanpub.com.
- In the Leanpub.com book admin area (attention: it has been completely revamped in July 2014), we need to find the page to paste the UA tracking ID into.
- The quickest way to get there is to go to the following address:
- Simply copy the above address into your browser’s address bar (you need to be logged into Leanpub for this to work instantly). Make sure that you substitute the
YOURBOOKNAMEpart of the address with the exact address part of your own book on Leanpub. If you’re not sure what to put here, read on.
- The Leanpub page that we’re looking for is also reachable by clicking around Leanpub’s web interface.
- Follow this sequence of links, starting from the upper right corner of the Leanpub.com homepage:
- In the
Your Accountdrop-down menu, click
- Click the
- Click the blue
Editbutton which appears to the left of the cover of your book that you want to track.
- In the left sidebar menu, click
Settings. It expands to show the
- On the Analytics page, paste the UA tracking ID you previously copied from Google Analytics into the field
Google Analytics Code (optional).
- Don’t forget to click the
Update Bookbutton. Congratulations, you have now successfully integrated your Leanpub book with your Google Analytics account!
Prerequisite #3: you have already published your book on Leanpub.
It makes no sense to track purchases before you have published the book on Leanpub. Publishing on Leanpub means that you have pressed the ‘Publish’ button in the Leanpub admin area, making your book ready for the readers to purchase. Here are Leanpub’s official instructions how to complete that final step of publishing on Leanpub.
If you’re not ready to publish just yet, it’s ok, this tutorial can wait. Bookmark it and return later.
Short instructions for advanced users who already know their way around Google Analytics
So you have pasted your UA tracking ID into Leanpub and have a book published? Let’s now do what you have come here for: let’s prepare a regular expression and create a Google Analytics goal with it.
- First edit this regular expression TEMPLATE:
BOOK_OR_BUNDLE_NAME with the exact slug of your book’ or bundle’s landing page at Leanpub.com. Do not include any slashes. A slug is the part of the URL that follows immediately after
https://leanpub.com/ (for books) or
https://leanpub.com/b/ (for bundles).
For example, my book’s landing page on Leanpub is this:
The slug for my book is this:
And my unique regular expression becomes this:
- Then create a goal in Google Analytics by visiting https://www.google.com/analytics/ and clicking this sequence of links:
- -> click
Adminin the main horizontal menu -> click
VIEWcolumn -> click the red
NEW GOALbutton -> click the
Customradio button in the
Goal setupsection -> type the name of your goal in the
Namefield, i.e. Book1 purchase -> under
Type, select the
Destinationradio button -> in Goal details, under
Destination, there’s a drop-down menu in which you must select
Regular expression-> paste your unique and modified regex
^(\/b)?\/BOOK_OR_BUNDLE_NAME\/(.*)\/thankyou$into the field to the right of the
Regular expressiondrop-down menu -> switch
Onand enter the dollar value, which should be the minimum purchase value of your book. Skip this step if you’re giving away your book for free. -> click the
Create Goalbutton, and you’re done with Google Analytics for now.
- Scroll down this tutorial to find more detailed, step-by-step instructions with screenshots on how to create a goal in Google Analytics.
- Test your setup. Scroll down this tutorial to find detailed instructions how to test easily.
Want to know the details about what this regular expression does and how it matches Leanpub URLs? At the end of this tutorial there’s a section with answers to this and similar advanced questions. In short, this regex is universal, it should cover tracking of all types of books and bundles on Leanpub, and it allows you to track each of your books separately.
Step by step instructions with screenshots (longer version for beginners)
We have now successfully integrated your book’s Leanpub pages with Google Analytics. Before we’re ready to create the goal in Google Analytics, let’s first prepare our regular expression so that we can easily copy and paste it into Google Analytics when we need it.
Step 1: preparing the regular expression
We’ve created a template of a regular expression that looks like one line of gibberish characters. Your job will be to edit one specific part of it, copy it to a safe place and then paste it into a certain field in Google Analytics when the time comes.
Here’s the deal: the following step is not hard at all. You just need to be extra careful not to delete, add, or mistype any characters. If you do, the tracking won’t work. That’s why people find regular expressions scary: errors are hard to identify because the regular expression itself is hard to read.
Don’t worry: at the end of this tutorial I show you exactly how to test your tracking so that you are sure that everything works great.
- Copy the following regular expression template and paste it into your favorite text editor such as Notepad or TextEdit for editing:
- You’re not done yet! You need to EDIT this part of the regular expression:
- You need to REPLACE that part with the exact address of your book’s landing page as it appears on leanpub.com!__
BOOK_OR_BUNDLE_NAMEis just a placeholder we use in this tutorial to show which part of the regular expression every book author needs to edit and adapt to his or her own situation.
- Let’s now find out what the exact address of your book on Leanpub.com is. In Leanpub’s admin area where you edit your book, in the menu in the left sidebar, click
Landing Pageto expand and display items in the submenu. Click
View Landing Page. You might want to click the little rectangle that is shown for this menu item to view your book’s landing page in a new window.
- You’ll see your book’s landing page. Take a look at the address of your book in the adress bar of your browser, it shows something like this:
https://leanpub.com/YOURBOOKNAME(if you have a book)
https://leanpub.com/b/YOURBUNDLENAME(if you have a bundle)
- Your book’s unique address under leanpub.com is this
YOURBUNDLENAMEpart, or everything that follows
https://leanpub.com/. That’s the part with which you need to replace the
BOOK_OR_BUNDLE_NAMEplaceholder in our regular expression template.
- Lost? Let’s take a look at an example.
- My book’s landing page is this:
- So, when I was editing the regular expression template for my purposes, I took this regular expression template:
- and I replaced
- My final result was this regular expression which I copied and pasted into Google Analytics:
- Want another, simpler example, where the unique book address contains no dashes (-)?
- Take this Leanpub book: The Agile Agency.
- Its landing page is this:
- The author of this book should take edit this regular expression template:
- and he should replace the
- His result would be this unique regular expression:
- In short: remove and replace only the
BOOK_OR_BUNDLE_NAMEpart of the template and don’t touch, delete, or edit any other character in the template. Double-check and triple-check that you didn’t unintentionally add extra slashes (/) or backslashes (); your unique book address should not contain any of those.
- If you find any of this too demanding, just show me your book on Leanpub in the comments to this tutorial and I’ll write a regular expression for you.
Step 2: creating a book purchase goal in Google Analytics
A goal is a set of instructions that tells Google Analytics which action to track. A book purchase is the action we will be tracking. Every time one of your readers purchases your book, Google Analytics will record that purchase as a conversion and show you all kinds of useful reports about that conversion.
- Access Google Analytics at https://www.google.com/analytics/ and click
Adminin the main menu.
- After you’ve made sure that you have selected the proper account from the drop-down menu in the left-most column
Viewcolumn (the right-most one).
- Click the red
+ NEW GOALbutton.
- In the
Goal setupsection, there are two radio buttons: the already preselected
Customand then click the
Next stepbutton to proceed.
- We’re now in the
Goal descriptionsection. First, Google Analytics wants you to name your goal properly so that you can recognize it among other goals you might set up later.
- In the
Namefield, enter an arbitrary name of the goal, for example
Book1 purchase. The name should be short and distinctive. If you want, you could give your goal a more descriptive name.
Type, select the
Destinationradio button and click the
Next stepbutton to proceed.
- We’re now in the
Goal detailssection where you will finally copy and paste the regular expression you have prepared!
Destination, click the drop-down menu where
Equals tois now displayed and select
- In the field next to this drop-down menu, paste the regular expression that you have previously prepared. Once again, you should replace the
BOOK_OR_BUNDLE_NAMEpart with the unique address of your book, as I explained before in this article.
- If your book is not free, set the
On(otherwise, skip this step). In the input field next to it, where you see
XYon the image above, I suggest you enter the minimum price at which you’re currently selling your book on Leanpub (you can always change this value later). For example, my books currenly sells at $26 and that’s what I entered in that field - number
- If you turn on this
Valueswitch, be aware that your data will not be completely accurate. Every purchase will be shown as having the fixed value that you entered, but not all transactions will be worth that exact value. For example, some people choose to buy your book at higher prices, and some people will use your discount coupons and buy at lower prices. Arguably, the average value will be your minimum price, so it makes sense to use the minimum price here as the best approximation.
- At this moment, it is not possible to track exact purchase values for Leanpub books (although Google Analytics is used for this exact purpose). At the end of this tutorial I explain why.
- Click the
Create Goalbutton and you’re done setting up conversion tracking in Google Analytics. You’ll see a screen like on the image below.
That wasn’t so hard, was it? You’re doing great!
Let’s now make sure that Google Analytics is really tracking our book sales.
How to test that everything works ok
To speed up the testing process, you’re going to now pretend you are a reader and you are going to purchase your book on Leanpub.
If your book is free, simply visit your book’s landing page and buy your own book. The purchase is complete once a page ending in
/thankyou loads in your browser’s address bar.
If your book is not free, the best way to test is to create a coupon for a free purchase on Leanpub. A coupon is actually a specially coded link that you generate using Leanpub’s admin interface.
To create a free purchase coupon for the purposes of testing Google Analytics tracking, follow the steps below.
- Go to the already familiar book edit page and in the left sidebar menu, click
Edit Couponpage will be loaded.
- In the
Discounted Priceinput field, enter
0.00. This enables you to buy your own book without spending real money.
- Set both the
Start dateand the
End dateto today, meaning that the coupon will expire tomorrow. We’ll test this today and there’s no need to keep this coupon active anymore. You can always edit the dates later if you ever need to re-test conversion tracking.
- Set the
Max # Usesto 5. That means that you can buy your book five times for testing purposes.
Note, you can leave a quick note for yourself for the future, i.e. enter “For Google Analytics Conversion Tracking Testing Purposes Only”. In the future you will probably create many coupons, so you’ll want to know which one is which.
Create Couponto save the coupon.
Now that you’ve created a free purchase coupon, use it to buy your own book for free. You need at least one real purchase to test the tracking you’ve set up.
- When you create a coupon, Leanpub takes you to the list of all coupons, like on the image below.
- In the
Coupon URLcolumn, you’ll see a link starting with http://leanpub.com - click it to be taken to your book’s landing page.
- You’ll notice that your book’s landing page looks a little bit different. It’s because you’re visiting a specially coded address for buying your book with a coupon. The big green
Buy Nowbutton on Leanpub will now be framed in yellow: click that button and complete the purchase.
- The purchase is complete once the page ending in
/thankyouloads in the address bar of your browser.
Let’s now return to Google Analytics to see whether or not it has logged our purchase.
Google Analytics reports data in near real-time. This allows you to see sales almost as they happen, within seconds or minutes.
- After logging in at https://www.google.com/analytics/, in the left sidebar menu click
Real-Timeand then click
Goal Hits (Last 30 min). You should see a list of your goals, similar to the image below. In your case, there will be just one goal named
Book1 purchase(or whatever you named it before). In my case that you see on the image below, there are two goals: one for tracking my newsletter subscriptions and the other one for tracking book purchases.
- In the
Goal Hits (Last 30 min)column there should be one conversion logged: that would be your purchase. If you see this conversion, congratulations! You have successfully completed this tutorial.
What to do if my test fails and I don’t see a conversion?
First, wait longer — up to 24 hours, especially if you’ve just created your Google Analytics account for the first time.
Second, make sure your UA tracking ID has been installed properly. Read this Google Analytics Help page for more information about what to check and how.
Third, go over all the steps in this tutorial once again. Double-check every step. Common mistakes are these:
- bad regular expression (maybe you accidentally deleted or added a character?)
- UA tracking ID not entered in the Leanpub admin area
- book not published.
Fourth, ask for help in the comments to this tutorial. I’ll be glad to help. Let me know if you get stuck on any of the steps. It’s possible that the screens you see do not match the images or instructions in this tutorial (Google or Leanpub might have upgraded their software since the time of publishing of this tutorial).
How to read Google Analytics reports to sell more books
There are hundreds of reports in Google Analytics and you have your whole career to obsess over them daily. But there are only three major areas important to every online entrepreneur:
- Acquisition metrics (how did you acquire your visitors, subscribers and buyers — a.k.a. traffic)
- Behavior metrics (what people do on your website)
- Outcome metrics (what people do before they leave your website)
For the purposes of this tutorial, I want to show you one simple report that displays the most relevant data in a single view, without any customization. I have chosen the All Traffic Acquisition report because it answers the following questions at a glance:
- Which sources are bringing in the most traffic?
- Which sources are keeping the most people on my landing page the longest?
- Which sources convert most traffic to book sales?
To view your All Traffic Acquisition report, log in to Google Analytics and click Acquisition -> All Traffic in the left sidebar menu.
You’ll see a table with data such as this:
What you’re looking at is a snapshot from my Google Analytics account which I use to track my book purchases. I’ve highlighted a couple of interesting data points.
Which data is important in this view?
- Source / Medium column shows me where people are coming from. They’re coming mostly from Google, social media, other websites, paid advertising, and my email newsletter.
- Sessions column shows me the number of visitors (roughly speaking), per online source.
- Bounce Rate, Pages / Session and Avg. Session Duration tell me how interested people are in my website content, and I can see how are various sources performing. For example, my email subscribers seem to be the most interested in my content. This tells me I should invest more time, money and effort in my newsletter.
- The three right-most columns (Goal 2 Conversion Rate, Goal 2 Completions, Goal 2 Value) are shown in this table because I’ve set up goals and conversion tracking. When you have successfully completed this tutorial, data such as this will show in your Google Analytics. For example, these three columns are telling me the value in dollars of each and every online source.
So, what do I, as an online entrepreneur, do with this data? The data helps me make smarter decisions about where should I be spending my time every day. It helps me see the results of my hypotheses such as “If I change the text of my Google AdWords ads, will my paid advertising start converting to book sales?”. Without the data coming from conversion tracking, I have no idea which changes to make and whether my changes are working or not.
Teach yourself Google Analytics
The All Traffic report is only one tiny example of what you can do with the data that this tutorial has just helped you gather. It is a highly simplified example and there is no room in this article to get me started on the challenges of digital analytics today.
If you want to dig deeper into the wonderful world of digital analytics, I recommend three sources of top-notch knowledge:
For those who want to know more
What does the regular expression in this tutorial do, exactly?
I thought you’d never ask!
^(\/b)?\/BOOK_OR_BUNDLE_NAME\/(.*)\/thankyou$ matches three distinctive types of web page addresses (URLs) on Leanpub.com. Here they are and the examples of their respective ‘thank you’ pages:
^(\/b)?part is here because all bundle URLs start with
/b, but book URLs don’t. Meaning,
/bat the beginning of the URL is optional. This part of the regular expression matches whether or not a URL starts with /b.
\/BOOK_OR_BUNDLE_NAME\/part matches exactly the URL of our book or bundle on leanpub.com.
(.*)\/thankyou$part instructs Google Analytics to match one or more characters until it finds the string
/thankyouat the end of the URL. Leanpub assigns a unique and random purchase ID to every transaction and puts that unique ID in the URL. That’s why our regular expression needs to: a) accommodate for all possible unique purchase IDs and b) be as simple, as universal, and as accurate as possible.
Which cases does this conversion tracking cover, exactly?
This regular expression has been written to accommodate all Leanpub books, packages and bundles. It is universal and currently all Leanpub authors can use it.
The following Leanpub use cases are supported, too:
- Authors who have written only one book
- Authors who have written more than one book can track each book separately
- Authors who have created bundles consisting of their own books or other authors’ books
- Authors who have created packages for their books
- Authors who track their book(s) using only one Google Analytics account
- Authors who track their book(s) using multiple Google Analytics accounts
- Buyers paying with credit card and with Paypal
- Buyers using coupons
Are there any cases where this tracking might not work properly?
Other than tracking not working at all, there is one case which deserves attention.
Many authors have their own website where they promote their books that they sell over Leanpub. This means that they have created book landing pages on their own websites. I have done the same too: this is my website landing page, and this is my Leanpub landing page. My website is my home and I naturally want to promote my book there, while Leanpub is my main bookstore where I put my book for sale.
The regular expression in this tutorial looks for all page addresses starting with
/BOOK_OR_BUNDLE_NAME/ and ending with
/thankyou. Every time Google Analytics catches that a page matching this condition has loaded, it will log a conversion.
If you’re using the same Google Analytics account to track your website and Leanpub landing pages, make sure that you have no pages starting with
/BOOK_OR_BUNDLE_NAME/ and having
/thankyou anywhere after that in the address. Avoid using
/thankyou as a partial page name anywhere on your website, because pages named like that might trigger an unwanted conversion in Google Analytics.
This means that your real sales conversions will still be tracked, but your Google Analytics might show more conversions than there actually are. If this is the case, simply rename the pages on your website so that they don’t containt
/thankyou and you should be fine.
Google Analytics supports eCommerce conversion tracking, which shows the exact purchase value. Why didn’t we set that up, instead of goals?
It’s because Leanpub still does not support eCommerce conversion tracking. When you were setting up your goal, I told you to use your book’s minimum price as your fixed goal value. Google Analytics can do much better that that, for example it can show you exactly how much a reader has paid, up to the last cent. We’ll have to wait until Leanpub fully implements eCommerce tracking.
Your next steps
Bookmark this page for later use if you still haven’t published your book and return to it when you’re ready to publish.
If you get stuck at any step in this tutorial, please do not hesitate to ask for assistance in the comments! Your comments will help improve these tutorial.
Originally published at leanpub.com.