How to load data from Mixpanel to Redshift

How to Extract data from Mixpanel

At this post, we will work with the Export API to allow us to export our 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 to perform an 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 from other sources, you have two choices.

The first one is to enrich the data of Mixpanel with data coming from other sources. The second one is to extract the data Mixpanel holds for you and load it on a data warehousing repository for further analysis. This post will consider the second case.

Mixpanel is evolving into a platform where apart from the analytics services it offers, you will also be able to build applications integrated with it.

As a web API, you can access it using tools like CURL or Postman or your favorite HTTP client for your choice’s language or framework. 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 time period

Events

  • events – get total, unique, or average data for a set of events over 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 from 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 over 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 time period
  • 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 from People Analytics. Let’s assume that we want to export our raw data 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:
BATCHFILE
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:

JSON
{"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 from the export API are updated every 24 hours, so you will always have access to the data from the previous day. After you extract all the information you need, you have to map it to your data warehouse repository schema and then load the data to it following this post’s instructions.

Prepare your Mixpanel Data for Amazon Redshift

Amazon Redshift is built around industry-standard SQL with added functionality to manage very large datasets and high-performance analysis. So, in order to load your data into it you will have to follow its data model, which is a typical relational database model. The data you extract from your data source should be mapped into tables and columns where you can consider the table as a map to the resource you want to store and columns the attributes of that resource. Also, each attribute should adhere to the data types that are supported by Redshift. Currently, the datatypes that are supported are the following:

  • SMALLINT
  • INTEGER
  • BIGINT
  • DECIMAL
  • REAL
  • DOUBLE PRECISION
  • BOOLEAN
  • CHAR
  • VARCHAR
  • DATE
  • TIMESTAMP

As your data are probably coming in a representation like JSON that supports a much smaller range of data types you have to be really careful about what data you feed into Redshift and make sure that you have mapped your types into one of the datatypes that are supported by Redshift.

Designing a Schema for Redshift and mapping the data from your data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database you need to load your data on one of the data sources that are supported as input by Redshift, these are the following:

How to Load data from Mixpanel to Redshift

The first step to load your Mixpanel data to Redshift is to put them in a source Redshift can pull it from. As mentioned earlier, there are three main data sources supported: Amazon S3, Amazon DynamoDB, and Amazon Kinesis Firehose, with Firehose being the most recent addition to insert data into Redshift.

To upload your data to Amazon S3 you will have to use the AWS REST API. As we see again APIs play an important role in both the extraction and the loading of data into our data warehouse. The first task that you have to perform is to create a bucket, you do that by executing an HTTP PUT on the Amazon AWS REST API endpoints for S3. You can do this by using a tool like CURL or Postman or use the libraries provided by Amazon for your favorite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation.

After you have created your bucket, you can start sending your data to Amazon S3, using the same AWS REST API but using the endpoints for Object operations. As in the Bucket case, you can either access the HTTP endpoints directly or use your preferred library.

DynamoDB imports data again from S3, it adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons you can avoid it.

Amazon Kinesis Firehose is the latest addition to insert data into Redshift and offers a real-time streaming approach to data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:

  1. create a delivery stream
  2. add data to the stream

Whenever you add new data to the stream, Kinesis takes care of adding these data to S3 or Redshift, again going through S3, in this case, is redundant if your goal is to move your data to Redshift. The execution of the previous two steps can be performed either through the REST API or through your favorite library, just as in the previous two cases. The difference here is that you’ll be using a Kinesis Agent for pushing your data into the stream.

Amazon Redshift supports two methods for loading data into it. The first one is by invoking an INSERT command. You can connect to your Amazon Redshift instance with your client, using either a JDBC or ODBC connection and then you perform an INSERT command for your data.

SH
insert into category_stage values
(12, 'Concerts', 'Comedy', 'All stand-up comedy performances');

The way you invoke the INSERT command is the same as you would do with any other SQL database. For more information, you can check the INSERT examples page on the Amazon Redshift documentation.

Redshift is not designed for INSERT-like operations. On the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from an Amazon DynamoDB table. When you perform COPY commands, Redshift can read multiple files simultaneously, automatically distribute the workload to the cluster nodes, and perform the load in parallel. As a command, COPY is quite flexible and allows for many different ways of using it, depending on your use case. Performing a COPY on Amazon S3 is as simple as the following command:

SH
copy listing
from 's3://mybucket/data/listing/'
credentials 'aws_access_key_id=;aws_secret_access_key=';

For more examples of invoking a COPY command, you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance using a JDBC or ODBC connection and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.

What is the best way to load data from Mixpanel to Amazon Redshift?

So far, we just scraped the surface of what can be done with Amazon Redshift and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from, and the requirements of your use case.

Things can get even more complicated if you want to integrate data coming from different sources. Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use a product like RudderStack to handle this kind of problem automatically.

RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics and more. Quickly and safely move all your data from Mixpanel into Amazon Redshift and start generating insights from your data.

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 Amazon Redshift.