Analyzing a Conversion Funnel in BigQuery Using Fivetran Powered Mixpanel Data

mixpanel-funnels_fqmumg.png

In a recent post I outlined how to use Fivetran to sync Mixpanel data to BigQuery for analysis in Looker. Today we’ll walk through how to write a SQL query to analyze a funnel using the Mixpanel data in BigQuery.

For this analysis, we’re going to create a three-step funnel showing how many visitors who start on Help Scout’s pricing page click through to the sign up page and then sign up.

One Step

To begin, lets just look at visitors who viewed the pricing page:

SELECT
distinct_id,
time AS viewed_pricing_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://www.helpscout.net/pricing/%"
+————————————————–+——————————–+
| distinct_id | viewed_pricing_at |
+————————————————–+——————————–+
| 1628dfc9f7e00184dcc4e71615c5a49a101628dfc9f800 | 20180403 17:09:27.000000 UTC |
| 16291bd9645601abc2a0f87a6c722c88016291bd9648ae | 20180404 10:41:32.000000 UTC |
| 162936a4e6f3701526b616b4b327d25800162936a4e738 | 20180404 18:27:46.000000 UTC |
| 162a0381d4810b3137c5860031d3c3d10d162a0381d496 | 20180407 06:10:35.000000 UTC |
+————————————————–+——————————–+

view raw
pricing-page.sql
hosted with ❤ by GitHub

You might wonder why we need the % at the end of the URL; that’s simply to make sure the results include pages with URL parameters such as https://www.helpscout.net/pricing/?utm_source=adwords.

Two Steps

Next, we’ll join these results on sign up page data, making sure that the sign up page views occurred after the pricing page views:

SELECT
pricing.distinct_id,
viewed_pricing_at,
viewed_sign_up_at
FROM (
SELECT
distinct_id,
time AS viewed_pricing_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://www.helpscout.net/pricing/%"
) pricing
LEFT JOIN (
SELECT
distinct_id,
time AS viewed_sign_up_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://secure.helpscout.net/members/register/%"
) sign_up_page ON sign_up_page.distinct_id = pricing.distinct_id AND sign_up_page.viewed_sign_up_at > viewed_pricing_at
+———————————————————+——————————–+——————————–+
| distinct_id | viewed_pricing_at | viewed_sign_up_at |
+———————————————————+——————————–+——————————–+
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:31:26.000000 UTC | 20180409 06:08:08.000000 UTC |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180405 12:48:37.000000 UTC | 20180409 06:08:08.000000 UTC |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:28:42.000000 UTC | 20180409 06:08:08.000000 UTC |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:27:49.000000 UTC | 20180409 06:08:08.000000 UTC |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:35:51.000000 UTC | 20180409 06:08:08.000000 UTC |
| 162914788c5fb09337b4c437dc4b34356b144000162914788c628 | 20180404 17:30:18.000000 UTC | |
+———————————————————+——————————–+——————————–+

view raw
two-step-funnel.sql
hosted with ❤ by GitHub

The reason we LEFT JOIN is because not all visitors will make it to the next step of the funnel and we want the data to reflect that.

Note that we join on both the distinct_id (to ensure each result is for a single visitor) and on the time the events occurred.

Three Steps

Extending this to the third and final step of the funnel, the Signed Up event, we get:

SELECT
pricing.distinct_id,
viewed_pricing_at,
viewed_sign_up_at,
signed_up_at
FROM (
SELECT
distinct_id,
time AS viewed_pricing_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://www.helpscout.net/pricing/%"
) pricing
LEFT JOIN (
SELECT
distinct_id,
time AS viewed_sign_up_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://secure.helpscout.net/members/register/%"
) sign_up_page ON sign_up_page.distinct_id = pricing.distinct_id AND sign_up_page.viewed_sign_up_at > viewed_pricing_at
LEFT JOIN (
SELECT
distinct_id,
time AS signed_up_at
FROM mp.event
WHERE
name = "Signed Up"
) signed_up ON signed_up.distinct_id = pricing.distinct_id AND signed_up.signed_up_at > viewed_sign_up_at
+———————————————————+——————————–+——————————–+——————————–+
| distinct_id | viewed_pricing_at | viewed_sign_up_at | signed_up_at |
+———————————————————+——————————–+——————————–+——————————–+
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:31:26.000000 UTC | 20180409 06:08:08.000000 UTC | |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180405 12:48:37.000000 UTC | 20180409 06:08:08.000000 UTC | |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:28:42.000000 UTC | 20180409 06:08:08.000000 UTC | |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:27:49.000000 UTC | 20180409 06:08:08.000000 UTC | |
| 1628be3f8ab2080678714f9f0ecb34356b1fa4001628be3f8b825 | 20180403 07:35:51.000000 UTC | 20180409 06:08:08.000000 UTC | |
| 162914788c5fb09337b4c437dc4b34356b144000162914788c628 | 20180404 17:30:18.000000 UTC | 20180404 17:31:13.000000 UTC | 20180404 17:34:51.000000 UTC |
| 162914788c5fb09337b4c437dc4b34356b144000162914788c628 | 20180404 08:31:16.000000 UTC | 20180404 17:24:52.000000 UTC | 20180404 17:34:51.000000 UTC |
+———————————————————+——————————–+——————————–+——————————–+

view raw
three-steps.sql
hosted with ❤ by GitHub

Determining the First Event Occurrence for Each Step

The query above will return every combination of pricing page views, sign up page views, and sign up events for each visitor. For our funnel though, we don’t care whether they loaded the pricing page or sign up page multiple times, we only care that they did it at all. So we modify the query to only return the first instance of each event for each visitor:

SELECT
pricing.distinct_id,
MIN(viewed_pricing_at) AS viewed_pricing_at,
MIN(viewed_sign_up_at) AS viewed_sign_up_at,
MIN(signed_up_at) AS signed_up_at
FROM (
SELECT
distinct_id,
time AS viewed_pricing_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://www.helpscout.net/pricing/%"
) pricing
LEFT JOIN (
SELECT
distinct_id,
time AS viewed_sign_up_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://secure.helpscout.net/members/register/%"
) sign_up_page ON sign_up_page.distinct_id = pricing.distinct_id AND sign_up_page.viewed_sign_up_at > viewed_pricing_at
LEFT JOIN (
SELECT
distinct_id,
time AS signed_up_at
FROM mp.event
WHERE
name = "Signed Up"
) signed_up ON signed_up.distinct_id = pricing.distinct_id AND signed_up.signed_up_at > viewed_sign_up_at
GROUP BY 1
+————————————————————+——————————–+——————————–+——————————–+
| distinct_id | viewed_pricing_at | viewed_sign_up_at | signed_up_at |
+————————————————————+——————————–+——————————–+——————————–+
| 162931933729b20b3ba365e6d865336c7b053840001629319337333b | 20180404 16:58:45.000000 UTC | 20180411 20:50:13.000000 UTC | 20180411 20:51:46.000000 UTC |
| 162b770945247a0145ce1cbf076d84958601fa400162b77094541286 | 20180411 19:56:18.000000 UTC | 20180411 20:02:54.000000 UTC | |
| 162899a5ec58950fc657f6c6ebde33697b07384000162899a5ec68c6 | 20180409 19:20:01.000000 UTC | 20180411 19:38:59.000000 UTC | |
| 162b73858df1f80625fc7a476d418763131183d10d162b73858e2237 | 20180411 17:19:53.000000 UTC | 20180411 17:23:22.000000 UTC | |
| 162b6d32dd11a50bbf34346ce9393553629231015162b6d32dd2646 | 20180411 15:30:09.000000 UTC | 20180411 16:40:08.000000 UTC | |
| 162875b83f63e20870a441baadb633697b07384000162875b83f714f | 20180402 10:15:44.000000 UTC | 20180411 14:56:37.000000 UTC | 20180411 14:57:27.000000 UTC |
+————————————————————+——————————–+——————————–+——————————–+

Measuring the Funnel

Finally, we count how many visitors made it to each step to get our funnel:

SELECT
COUNT(viewed_pricing_at) AS viewed_pricing,
COUNT(viewed_sign_up_at) AS viewed_sign_up,
COUNT(signed_up_at) AS signed_up
FROM (
SELECT
pricing.distinct_id,
MIN(viewed_pricing_at) AS viewed_pricing_at,
MIN(viewed_sign_up_at) AS viewed_sign_up_at,
MIN(signed_up_at) AS signed_up_at
FROM (
SELECT
distinct_id,
time AS viewed_pricing_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://www.helpscout.net/pricing/%"
) pricing
LEFT JOIN (
SELECT
distinct_id,
time AS viewed_sign_up_at
FROM mp.event
WHERE
name = "Viewed Page" AND
current_url LIKE "https://secure.helpscout.net/members/register/%"
) sign_up_page ON sign_up_page.distinct_id = pricing.distinct_id AND sign_up_page.viewed_sign_up_at > viewed_pricing_at
LEFT JOIN (
SELECT
distinct_id,
time AS signed_up_at
FROM mp.event
WHERE
name = "Signed Up"
) signed_up ON signed_up.distinct_id = pricing.distinct_id AND signed_up.signed_up_at > viewed_sign_up_at
GROUP BY 1
) visitor_data
+————–+—————-+———–+
| viewed_pricing | viewed_sign_up | signed_up |
+————–+—————-+———–+
| 1234 | 567 | 89 |
+————–+—————-+———–+

Voila!

From here, you can take it a step further and segment the results on a property in the first step of the funnel or add a funnel duration to limit how long visitors have to complete the funnel, etc.

If you have any questions about this, don’t hesitate to reach out.

Querying Mixpanel using JQL from the Terminal

jql-terminal.png

Mixpanel provides a way to query the data you send to it using something they call JavaScript Query Language (JQL).

To experiment with it, you can navigate to the JQL app for your project:

Screen Shot 2018-04-09 at 4.07.17 PM.png

Which takes you to the JQL editor:

Screen Shot 2018-04-09 at 4.07.50 PM.png

Alternatively, you query Mixpanel’s JQL endpoint directly from a local script:

First, you need to create a file with the JQL. I called mine testing.js:

function main() {
return Events({
from_date: '2018-04-09',
to_date: '2018-04-09',
event_selectors: [{event: 'Signed Up'}]
});
}

view raw
testing.js
hosted with ❤ by GitHub

Next, grab your project’s API Secret from its settings.

Finally, execute the JQL:

$ curl --silent https://mixpanel.com/api/2.0/jql -u YOUR_API_KEY: --data-urlencode script@testing.js | python -m json.tool

The last part that pipes to python simply formats the JSON response when it’s displayed in the terminal for easy viewing:

[
{
"dataset": "$mixpanel",
"distinct_id": "162a9bea91d1cb-01213631147e12-3f636c4c-1aeaa0-162a9bea91e8cf",
"labels": [],
"name": "Signed Up",
"properties": {
"$browser": "Safari",
"$browser_version": 11.1,
"$city": "Verrieres-le-Buisson",
"$current_url": "https://secure.helpscout.net/welcome/segmentation-questions/",
"$initial_referrer": "$direct",
"$initial_referring_domain": "$direct",
"$lib_version": "2.20.0",
"$os": "Mac OS X",
"$referrer": "https://secure.helpscout.net/members/verification-code/",
"$referring_domain": "secure.helpscout.net",
"$region": "Essonne",
"$screen_height": 1050,
"$screen_width": 1680,
"com_id": 1234,
"mp_country_code": "FR",
"mp_lib": "web"
},
"sampling_factor": 1,
"time": 1523241142000
}
]

view raw
jql-response.json
hosted with ❤ by GitHub

This is a trivial example obviously, but there’s a lot more you can do with JQL. You can use JQL to perform any type of analysis that you can in Mixpanel itself, and more.


			

Tracking What Pages Your Visitors View Prior to Signing Up Using Mixpanel, Fivetran, BigQuery, and Looker

setup.png

One of the things we’ve been analyzing at Help Scout recently is what paths individual companies take before signing up for a trial. For example, looking at a company that signed up last month, did they start their journey with Help Scout on our homepage? Or did they find us via our blog? Or one of our marketing landing pages? And once we know that, what other pages did they visit before signing up? Did that company wind up becoming a customer? How much did we make from them?

This post is about how we’ve wrangled the data using Mixpanel, Fivetran, BigQuery, and Looker to help us answer these questions.

Big picture, we’re tracking page view and sign up events in Mixpanel, syncing that data to BigQuery using Fivetran, then tying it to our internal company data in Looker for easy analysis.

Step 1: Tracking Page Views and Sign Ups in Mixpanel

If you’ve used an event-based analytics service, this step will be pretty straightforward. We load the Mixpanel script on every page, then fire a Viewed Page event:

<script>
mixpanel.track("Viewed Page");
</script>

view raw
viewed-page.js
hosted with ❤ by GitHub

And then if the company signs up for a trial, we fire a Signed Up event with that company’s id as a property:

mixpanel.track("Signed Up", {
com_id: comId
});

view raw
signed-up.js
hosted with ❤ by GitHub

These are the only two Mixpanel events we track. If we wanted to track actions in-app, we could also fire custom events for those, but we don’t at Help Scout because we tie this Mixpanel data together with our internal data about what companies have done in-app, eliminating the need for additional Mixpanel events.

Here’s what the Viewed Page event looks like in Mixpanel’s Live View:

Screen Shot 2018-04-06 at 1.42.07 PM.png

Also as we’ll see later, Mixpanel automatically tracks a lot of details about the visitor: things like the browser, country, URL, referrer, OS, etc, all of which we can use use in our analyses.

Step 2: Syncing Mixpanel data to BigQuery with Fivetran

Fivetran is this amazing service that specializes in helping you centralize all of your data in a data warehouse. For example, we have Fivetran connectors set up for MySQL (which we use internally at Help Scout), Salesforce, HubSpot, Google Sheets, and now Mixpanel:

Screen Shot 2018-04-06 at 1.46.42 PM.png

Screen Shot 2018-04-06 at 1.51.58 PM.png

Taking Mixpanel as an example, we provide Fivetran out Mixpanel API credentials, then Fivetran queries Mixpanel’s API periodically, cleans up the results, and throws it all in BigQuery:

Screen Shot 2018-04-06 at 1.50.21 PM.png

This lets us analyze our Mixpanel data just like we would other SQL data:

SELECT COUNT(*) FROM mp.event WHERE city = "Tokyo" AND DATE(time) = "2018-04-03"

view raw
tokyo.sql
hosted with ❤ by GitHub

We can also access the custom event properties that we’re tracking for the Signed Up event using Standard SQL’s json_extract_scalar function:

SELECT
properties,
JSON_EXTRACT_SCALAR(properties, "$.com_id") AS com_id
FROM mp.event
WHERE name = "Signed Up"
LIMIT 1
+—————+——–+
| properties | com_id |
+—————+——–+
| {"com_id":1234} | 1234 |
+—————+——–+

view raw
com-id.sql
hosted with ❤ by GitHub

Once we have the company id, it’s just a matter of querying for a specific company id to view their event history:

SELECT
time,
name,
current_url
FROM (
SELECT
distinct_id,
CAST(JSON_EXTRACT_SCALAR(properties, "$.com_id") AS INT64) AS com_id
FROM mp.event
WHERE name = "Signed Up"
) signed_up
JOIN mp.event ON event.distinct_id = signed_up.distinct_id
WHERE signed_up.com_id = 1234
ORDER BY time ASC
+——————————+————-+————————————————————–+
| time | name | current_url |
+——————————+————-+————————————————————–+
| 20180405 15:38:38.000000 UTC | Viewed Page | https://www.helpscout.net/ |
| 20180405 15:38:40.000000 UTC | Viewed Page | https://secure.helpscout.net/members/login/ |
| 20180405 15:38:46.000000 UTC | Viewed Page | https://www.helpscout.net/pricing/ |
| 20180405 15:38:52.000000 UTC | Viewed Page | https://secure.helpscout.net/members/register/19/ |
| 20180405 15:39:43.000000 UTC | Signed Up | https://secure.helpscout.net/welcome/segmentationquestions/ |
+——————————+————-+————————————————————–+

view raw
visitor-flow.sql
hosted with ❤ by GitHub

Step 3: Modeling the data in Looker

If you have access to a Business Intelligence tool like Looker, you can model this Mixpanel data and how to join it with your other data.

First, create a view to model the Mixpanel event data:

view: mp_events {
sql_table_name: mp.event ;;
dimension: event_id {
primary_key: yes
type: number
sql: ${TABLE}.event_id ;;
}
dimension: browser {
type: string
sql: ${TABLE}.browser ;;
}
dimension: browser_version {
type: number
sql: ${TABLE}.browser_version ;;
}
dimension: city {
type: string
sql: ${TABLE}.city ;;
}
dimension: full_url {
type: string
sql: ${TABLE}.current_url ;;
}
dimension: has_url_parameters {
type: yesno
hidden: yes
sql: STRPOS(${full_url}, "?") > 0 ;;
}
dimension: current_url_without_parameters {
hidden: yes
label: "Current URL without Parameters"
type: string
sql:
CASE
WHEN ${has_url_parameters} THEN SUBSTR(${full_url}, 0, STRPOS(${full_url}, "?") – 1)
ELSE ${full_url}
END ;;
}
dimension: has_url_hash {
type: yesno
hidden: yes
sql: STRPOS(${current_url_without_parameters}, "#") > 0 ;;
}
dimension: current_url {
type: string
sql:
CASE
WHEN ${has_url_hash} THEN SUBSTR(${current_url_without_parameters}, 0, STRPOS(${current_url_without_parameters}, "#") – 1)
ELSE ${current_url_without_parameters}
END ;;
}
dimension: current_url_path {
type: string
sql: SUBSTR(${current_url}, STRPOS(${current_url}, "helpscout.net") + LENGTH("helpscout.net")) ;;
}
dimension: device {
type: string
sql: ${TABLE}.device ;;
}
dimension: distinct_id {
type: string
sql: ${TABLE}.distinct_id ;;
}
dimension: initial_referrer {
type: string
sql: ${TABLE}.initial_referrer ;;
}
dimension: initial_referring_domain {
type: string
sql: ${TABLE}.initial_referring_domain ;;
}
dimension: lib_version {
type: string
sql: ${TABLE}.lib_version ;;
}
dimension: mp_country_code {
type: string
sql: ${TABLE}.mp_country_code ;;
}
dimension: mp_lib {
type: string
sql: ${TABLE}.mp_lib ;;
}
dimension: name {
type: string
sql: ${TABLE}.name ;;
}
dimension: os {
type: string
sql: ${TABLE}.os ;;
}
dimension: properties {
type: string
sql: ${TABLE}.properties ;;
}
dimension: referrer {
type: string
sql: ${TABLE}.referrer ;;
}
dimension: referring_domain {
type: string
sql: ${TABLE}.referring_domain ;;
}
dimension: region {
type: string
sql: ${TABLE}.region ;;
}
dimension: screen_height {
type: number
sql: ${TABLE}.screen_height ;;
}
dimension: screen_width {
type: number
sql: ${TABLE}.screen_width ;;
}
dimension: search_engine {
type: string
sql: ${TABLE}.search_engine ;;
}
dimension_group: time {
label: "Event"
type: time
timeframes: [
raw,
time,
date,
week,
month,
quarter,
year
]
sql: ${TABLE}.time ;;
}
dimension: utm_source {
label: "UTM Source"
type: string
sql: JSON_EXTRACT_SCALAR(${properties}, "$.utm_source") ;;
}
dimension: utm_campaign {
label: "UTM Campaign"
type: string
sql: JSON_EXTRACT_SCALAR(${properties}, "$.utm_campaign") ;;
}
dimension: utm_term {
label: "UTM Term"
type: string
sql: JSON_EXTRACT_SCALAR(${properties}, "$.utm_term") ;;
}
dimension: utm_medium {
label: "UTM Medium"
type: string
sql: JSON_EXTRACT_SCALAR(${properties}, "$.utm_medium") ;;
}
dimension: utm_content {
label: "UTM Content"
type: string
sql: JSON_EXTRACT_SCALAR(${properties}, "$.utm_content") ;;
}
measure: count {
label: "Total Events"
type: count
drill_fields: [event_id, name]
}
measure: unique_visitors {
type: count_distinct
sql: ${distinct_id} ;;
}
}

view raw
mp_events.lookml
hosted with ❤ by GitHub

Then create a view with a derived table to model the relationship between a distinct_id in Mixpanel with a company id:

view: mp_companies {
derived_table: {
sql:
SELECT
distinct_id,
CAST(JSON_EXTRACT_SCALAR(properties, "$.com_id") AS INT64) AS com_id
FROM mp.event
WHERE name = "Signed Up" ;;
}
dimension: distinct_id {
type: number
sql: ${TABLE}.distinct_id ;;
hidden: yes
}
dimension: com_id {
label: "Company ID"
type: number
sql: ${TABLE}.com_id ;;
}
}

view raw
mp-companies.lookml
hosted with ❤ by GitHub

Finally, connect those two views and any others you want to be able to analyze together:

connection: "bigquery"
include: "*.view.lkml"
explore: mp_events {
view_label: "Mixpanel"
label: "Mixpanel"
sql_always_where: STRPOS(${full_url}, "https://www.helpscout.net&quot;) = 1 OR STRPOS(${full_url}, "https://secure.helpscout.net&quot;) = 1 ;;
join: mp_companies {
view_label: "Mixpanel"
type: left_outer
relationship: many_to_one
sql_on: ${mp_companies.distinct_id} = ${mp_events.distinct_id} ;;
}
join: helpscout_companies {
view_label: "Company"
relationship: one_to_one
sql_on: ${helpscout_companies.com_id} = ${mp_companies.com_id} ;;
}
}

view raw
mp.lookml
hosted with ❤ by GitHub

Step 4: Analyzing the data in Looker

Once you’ve modeled the data, you can analyze the event history for specific companies, track trends like page views and unique visitors, and more:

Screen Shot 2018-04-06 at 2.28.57 PM.png

In future posts I’ll walk through some of the other interesting analyses you can perform with this data.

If you have any questions about this setup, don’t hesitate to reach out.