Find Out Where Your Leanpub Book Sales Are Coming From (a.k.a. Setting Up Conversion Tracking in Google Analytics With Regular Expressions)

Feb 22, 2017 · 19 min read

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, her company’s upcoming productivity software for web development agencies.

Image for post

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.

  1. First we need to locate the UA tracking ID in Google Analytics. Log in to Google Analytics at and click Admin.
  2. You will see something like this:
Image for post
  1. 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.
  2. In the middle column Property, click Tracking Info and then click Tracking Code.
Image for post
  1. 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.
  2. That’s everything we need to do in Google Analytics for now. Let’s now switch to
  3. In the 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.
  4. The quickest way to get there is to go to the following address:
  6. 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 YOURBOOKNAME part 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.
  7. The Leanpub page that we’re looking for is also reachable by clicking around Leanpub’s web interface.
  8. Follow this sequence of links, starting from the upper right corner of the homepage:
  9. In the Your Account drop-down menu, click Dashboard.
Image for post
  1. Click the Books tab.
Image for post
  1. Click the blue Edit button which appears to the left of the cover of your book that you want to track.
Image for post
  1. In the left sidebar menu, click Settings. It expands to show the Analytics option. Click Analytics.
Image for post
  1. On the Analytics page, paste the UA tracking ID you previously copied from Google Analytics into the field Google Analytics Code (optional).
  2. Don’t forget to click the Update Book button. 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.

  1. First edit this regular expression TEMPLATE:


Replace BOOK_OR_BUNDLE_NAME with the exact slug of your book’ or bundle’s landing page at Do not include any slashes. A slug is the part of the URL that follows immediately after (for books) or (for bundles).

For example, my book’s landing page on Leanpub is this:

The slug for my book is this: recurring-revenue-web-agencies

And my unique regular expression becomes this: ^(\/b)?\/recurring-revenue-web-agencies\/(.*)\/thankyou$

  1. Then create a goal in Google Analytics by visiting and clicking this sequence of links:
  2. -> click Admin in the main horizontal menu -> click Goals in the VIEW column -> click the red NEW GOAL button -> click the Custom radio button in the Goal setup section -> type the name of your goal in the Name field, i.e. Book1 purchase -> under Type, select the Destination radio 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 expression drop-down menu -> switch Value to On and 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 Goal button, and you’re done with Google Analytics for now.
  3. Scroll down this tutorial to find more detailed, step-by-step instructions with screenshots on how to create a goal in Google Analytics.
  4. 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.

  1. Copy the following regular expression template and paste it into your favorite text editor such as Notepad or TextEdit for editing:
  2. ^(\/b)?\/BOOK_OR_BUNDLE_NAME\/(.*)\/thankyou$
  3. You’re not done yet! You need to EDIT this part of the regular expression:
  5. You need to REPLACE that part with the exact address of your book’s landing page as it appears on!__
  6. BOOK_OR_BUNDLE_NAME is 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.
  7. Let’s now find out what the exact address of your book on is. In Leanpub’s admin area where you edit your book, in the menu in the left sidebar, click Landing Page to 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.
Image for post
  1. 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:
  2. (if you have a book)
  3. or
  4. (if you have a bundle)
  5. Your book’s unique address under is this YOURBOOKNAME or YOURBUNDLENAME part, or everything that follows That’s the part with which you need to replace the BOOK_OR_BUNDLE_NAME placeholder in our regular expression template.
  6. Lost? Let’s take a look at an example.
  7. My book’s landing page is this:
  8. So, when I was editing the regular expression template for my purposes, I took this regular expression template: ^(\/b)?\/BOOK_OR_BUNDLE_NAME\/(.*)\/thankyou$
  9. and I replaced BOOK_OR_BUNDLE_NAME with recurring-revenue-web-agencies
  10. My final result was this regular expression which I copied and pasted into Google Analytics: ^(\/b)?\/recurring-revenue-web-agencies\/(.*)\/thankyou$
  11. Want another, simpler example, where the unique book address contains no dashes (-)?
  12. Take this Leanpub book: The Agile Agency.
  13. Its landing page is this:
  14. The author of this book should take edit this regular expression template: ^(\/b)?\/BOOK_OR_BUNDLE_NAME\/(.*)\/thankyou$
  15. and he should replace the BOOK_OR_BUNDLE_NAME part with theagileagency
  16. His result would be this unique regular expression: ^(\/b)?\/theagileagency\/(.*)\/thankyou$
  17. In short: remove and replace only the BOOK_OR_BUNDLE_NAME part 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.
  18. 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.

  1. Access Google Analytics at and click Admin in the main menu.
  2. After you’ve made sure that you have selected the proper account from the drop-down menu in the left-most column Account, click Goals in the View column (the right-most one).
Image for post
  1. Click the red + NEW GOAL button.
Image for post
  1. In the Goal setup section, there are two radio buttons: the already preselected Template and Custom. Click Custom and then click the Next step button to proceed.
Image for post
  1. We’re now in the Goal description section. First, Google Analytics wants you to name your goal properly so that you can recognize it among other goals you might set up later.
Image for post
  1. In the Name field, 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.
  2. Under Type, select the Destination radio button and click the Next step button to proceed.
  3. We’re now in the Goal details section where you will finally copy and paste the regular expression you have prepared!
Image for post
  1. Under Destination, click the drop-down menu where Equals to is now displayed and select Regular expression.
  2. 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_NAME part with the unique address of your book, as I explained before in this article.
  3. If your book is not free, set the Value switch to On (otherwise, skip this step). In the input field next to it, where you see XY on 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 26.
  4. If you turn on this Value switch, 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.
  5. 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.
  6. Click the Create Goal button and you’re done setting up conversion tracking in Google Analytics. You’ll see a screen like on the image below.
Image for post

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.

  1. Go to the already familiar book edit page and in the left sidebar menu, click Coupons. An Edit Coupon page will be loaded.
Image for post
  1. In the Discounted Price input field, enter 0.00. This enables you to buy your own book without spending real money.
  2. Set both the Start date and the End date to 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.
  3. Set the Max # Uses to 5. That means that you can buy your book five times for testing purposes.
  4. Under 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.
  5. Click Create Coupon to 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.

  1. When you create a coupon, Leanpub takes you to the list of all coupons, like on the image below.
Image for post
  1. In the Coupon URL column, you’ll see a link starting with - click it to be taken to your book’s landing page.
  2. 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 Now button on Leanpub will now be framed in yellow: click that button and complete the purchase.
  3. The purchase is complete once the page ending in /thankyou loads 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.

  1. After logging in at, in the left sidebar menu click Real-Time and then click Conversions.
  2. Under Viewing: 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.
Image for post
  1. 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:

  1. Acquisition metrics (how did you acquire your visitors, subscribers and buyers — a.k.a. traffic)
  2. Behavior metrics (what people do on your website)
  3. 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:

Image for post

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 Here they are and the examples of their respective ‘thank you’ pages:




  1. The ^(\/b)? part is here because all bundle URLs start with /b, but book URLs don’t. Meaning, /b at the beginning of the URL is optional. This part of the regular expression matches whether or not a URL starts with /b.
  2. The \/BOOK_OR_BUNDLE_NAME\/ part matches exactly the URL of our book or bundle on
  3. The (.*)\/thankyou$ part instructs Google Analytics to match one or more characters until it finds the string /thankyou at 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:

  1. Authors who have written only one book
  2. Authors who have written more than one book can track each book separately
  3. Authors who have created bundles consisting of their own books or other authors’ books
  4. Authors who have created packages for their books
  5. Authors who track their book(s) using only one Google Analytics account
  6. Authors who track their book(s) using multiple Google Analytics accounts
  7. Buyers paying with credit card and with Paypal
  8. 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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store