How to load data from Mixpanel to Google BigQuery

Extract data from Mixpanel

Mixpanel is an analytics-as-a-service application so naturally, it requires data to offer its analytics features. We usually think of it as a consumer of data and not a place where we would get data out of, in order to perform the analysis. But Mixpanel collects a lot of data related to how your customers use your product and in the case where you would like to do anything that also involves data by other sources you really have two choices. The first one is to enrich the data of Mixpanel with data coming from other sources and the second one is to extract any data Mixpanel holds for you and load it on a data warehousing repository for further analysis. In this post, I will consider the second case.

Mixpanel is evolving into a platform where apart from the analytics services that it offers, you will also be able to build applications that are integrated with it. In this post, we will work only with the Export API which purpose is to allow us to export our data from Mixpanel.

As a web API, you can access it by using tools like CURL or Postman or your favorite http client for the language or framework of your choice. Some options are the following:

Or you can use the libraries/SDKs that Mixpanel offers for the following languages:

  • Python
  • PHP
  • Ruby
  • Javascript

As a RESTful API it offers the following resources that you can interact with:

Annotations

  • annotations– list the annotations for a specified date range.
  • create– create an annotation
  • update– update an annotation
  • delete– delete an annotation

Export

  • export– get a “raw dump” of tracked events over a specific time period

Events

  • events– get total, unique, or average data for a set of events during a time period
  • top– get the top events from the last day
  • names– get the top event names for a time period

Event Properties

  • properties– get total, unique, or average data by a single event property
  • top– get the top properties for an event
  • values– get the top values for a single event property

Funnels

  • funnels– get data for a set of funnels in a time period
  • list– get a list of the names of all the funnels

Segmentation

  • segmentation– get data for an event, segmented and filtered by properties over a period of time
  • numeric– get numeric data, divided up into buckets for an event segmented and filtered by properties over a time period
  • sum– get the sum of a segment’s values per time unit
  • average– get the average of a segment’s values per time unit
  • Segmentation Expressions– a detailed overview of what a segmentation expression consists of

Retention

  • retention– get data about how often people are coming back (cohort analysis)
  • addiction– get data about how frequently people are performing events

People Analytics

  • engage– get data coming from People Analytics

Let’s assume that we want to export our raw data coming from Mixpanel. To do so we’ll need to execute requests to the export endpoint. An example of a request that would get us back raw events from Mixapanel looks like this:

HTML
https://data.mixpanel.com/api/2.0/export/?from_date=2012-02-14&expire=1329760783&sig=bbe4be1e144d6d6376ef5484745aac45 &to_date=2012-02-14&api_key=f0aa346688cee071cd85d857285a3464& where=properties%5B%22%24os%22%5D+%3D%3D+%22Linux%22&event=%5B%22Viewed+report%22%5D

The returned result is always in JSON serialization with one event per line sorted by increasing timestamp. It looks like the following sample:

JAVASCRIPT
{"event":"Viewed report", "properties":{"distinct_id":"foo","time":1329263748,"origin":"invite", "origin_referrer":"https://mixpanel.com/projects/","$initial_referring_domain":"mixpanel.com", "$referrer":"https://mixpanel.com/report/3/stream/","$initial_referrer":"https://mixpanel.com", "$referring_domain":"mixpanel.com","$os":"Linux","origin_domain":"mixpanel.com","tab":"stream", "$browser":"Chrome","Project ID":"3","mp_country_code":"US"}}

Important: Data originating from the export API are updated every 24 hours, so you will always have access to the data by the previous day.

After you extract all the information you need, you have to map it to the schema of your data warehouse repository and then load the data to it following the instructions of this post.

Prepare your Mixpanel Data for Google BigQuery

Before you load your data into BigQuery, you should make sure that it is presented in a format supported by it, so for example if the API you pull data out of returns XML you have to first transform it into a serialization that BigQuery understands. Currently, two data formats are supported:

You also need to make sure that the data types you are using are the ones supported by BigQuery, which are the following:

  • STRING
  • INTEGER
  • FLOAT
  • BOOLEAN
  • RECORD
  • TIMESTAMP

For more information please check the Preparing Data for BigQuery page on the documentation.

Load from Mixpanel to Google BigQuery

If you want to load data out of Mixpanel to Google BigQuery, you have to use one of the following supported data sources.

  1. Google Cloud Storage
  2. Sent data directly to BigQuery with a POST request
  3. Google Cloud Datastore Backup
  4. Streaming insert
  5. App Engine log files
  6. Cloud Storage logs

From the above list of sources, 5 and 6 are not applicable in our case.

For Google Cloud Storage, you first have to load your data into it, there are a few options on how to do this, for example you can use the console directly as it is described here and do not forget to follow the best practices. Another option is to post your data through the JSON API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. In its simplest case, it’s just a matter of one HTTP POST request using a tool like CURL or Postman. It should look like the following example.

JAVASCRIPT
POST /upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject HTTP/1.1 Host: www.googleapis.com Content-Type: application/text Content-Length: number_of_bytes_in_file Authorization: Bearer your_auth_token your Mixpanel data

and if everything went ok, you should get something like the following as a response from the server:

JAVASCRIPT
HTTP/1.1 200 Content-Type: application/json { "name": "myObject" }

Working with Curl or Postman is good only for testing, in case you want to automate the process of loading your data into Bigquery, you should write some code to send your data to Google Cloud Storage. In case you are developing on the Google App Engine you can use the library that is available for the languages that are supported by it:

  1. Python
  2. Java
  3. PHP
  4. Go

If you are using one of the above languages and you are not coding for the Google App Engine, you can use it to access the Cloud Storage from your environment. Interacting such a feature-rich product like Google Cloud Storage can become quite complicated depending on your use case, for more details on the different options that exist you can check Google Cloud Storage documentation. If you are looking for a less engaged and more neutral way of using Storage in the Cloud, you can consider a solution like RudderStack.

After you have loaded your data into Google Cloud Storage, you have to create a Load Job for BigQuery to actually load data into it, this Job should point to the source data in Storage on the Cloud that have to be imported, this happens by providing source URIs that point to the appropriate objects.

The previous method described, used a POST request to the Google Cloud Storage API for storing the data there and then load it into BigQuery. Another way to go is to do a direct HTTP POST request to BigQuery with data you would like to query. This approach is similar to how we loaded any data to Cloud in the Google Storage through the JSON API, but it uses the appropriate end-points of BigQuery to load every data there directly. The way to interact with it is quite similar, for more information can be found on the BigQuery API Reference and on the page that describes how you can load data into BigQuery using POST. You can interact with it using the HTTP client library of the language or framework of your choice, a few options are:

The Best way to Load Data from Mixpanel to BigQuery

So far, we just scraped the surface of what you can do with BigQuery and you can load data into it. Things can get even more complicated if you want to integrate data coming from different sources.

Are you striving to achieve results right now?

Instead of writing, hosting, and maintaining a flexible data infrastructure use RudderStack that can handle everything automatically for you.

RudderStack with one click integrates with sources or services, creates analytics-ready data, and syncs your Mixpanel to BigQuery right away. Don't want to go through the pain of direct integration? RudderStack’s Mixpanel to Google BigQuery integration makes it easy to send data from Mixpanel to Google BigQuery.

Sign Up For Free And Start Sending Data
Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.
Don't want to go through the pain of direct integration? RudderStack's Mixpanel integration makes it easy to send data from Mixpanel to Google BigQuery.