How to load data from Mixpanel to Redshift

Blendo
Blend your data world
9 min readApr 14, 2016

There are many reasons why you would like to load data from Mixpanel to Redshift. The purpose of this guide is to help you define a process or pipeline, to load your data from Mixpanel to Amazon Redshift for further analysis.

Let’s see how to access and extract your data from Mixpanel through its API and how to load it into Redshift, this process requires from you to write the code to get the data and make sure that this process will run every time new data are generated. Alternatively you can use products like Blendo that can handle this kind of problems automatically for you.

About Mixpanel

Mixpanel helps businesses grow by helping them understand how their users behave and use their products by tracking actions people take rather than page views. Mixpanel’s mission is to help the world learn from its data. It helps you to make your product better by measuring actions, instead of page views. Mixpanel gives you the ability to easily measure what people are doing in your app on iOS, Android, and web.

About Amazon Redshift

Amazon Redshift is one of the most popular data warehousing solutions which is part of the Amazon Web Services (AWS) ecosystem. It is a petabyte scale, fully managed data warehouse as a service solution that runs on the cloud. It is SQL based and you can communicate with it as you would do with PostgreSQL, actually you can use the same driver although it would be better to use the driversrecommended by Amazon. You can connect either through JDBC or ODBC connections.

How to Extract data from Mixpanel

At this post we will work with the Export API which purpose is 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 from in order to do perform 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 involves also data from others 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 the data Mixpanel holds for you and load it on a data warehousing repository for further analysis. In this post 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.

As a web API, you can access it using 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:

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:
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:

{"event":"Viewed report",
"properties":{"distinct_id":"foo","time":1329263748,"origin":"invite", "origin_referrer":"http://mixpanel.com/projects/","$initial_referring_domain":"mixpanel.com", "$referrer":"https://mixpanel.com/report/3/stream/","$initial_referrer":"http://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 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 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 datatypes 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 is 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 that 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 datasources 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 that Redshift can pull it from. As it was mentioned earlier there are three main data sources supported, Amazon S3, Amazon DynamoDB and Amazon Kinesis Firehose, with Firehose being the most recent addition as a way 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 but also 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 favourite 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 again the same AWS REST API but by using the endpoints for Object operations. As in the Bucket case you can either access the HTTP endpoints directly or use the library of your preference.

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 as a way to insert data into Redshift and offers a real time streaming approach into 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 favourite library just as in the previous two cases. The difference here is that for pushing your data into the stream you’ll be using a Kinesis Agent.

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.

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 is able to read multiple files in simultaneously and it automatically distributes the workload to the cluster nodes and performs 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:

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

For more examples on how to invoke 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. A possible alternative, instead of writing, hosting and maintaining a flexible data infrastructure, is to use a product like Blendo that can handle this kind of problems automatically for you.

Blendo integrates with Mixpanel and even more sources or services like databases, CRM, email campaigns, analytics. Quickly and safely move all your data from Mixpanel into Amazon Redshift or join data with other services like Mixpanel and Intercom or Mixpanel and Mailchimp and start generating insights from your data.

Originally published at blog.blendo.co.

--

--

Blendo
Blend your data world

Blendo is a self-serve platform that helps companies to build, maintain and use a single view of all the data they have access to. https://www.blendo.co/