How to load data from Mixpanel to SQL Data Warehouse
Extract data from Mixpanel
Mixpanel is an analytics-as-a-service application, so it requires data to offer its analytics features. Mixpanel collects huge amounts 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 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. This post will consider the second case.
Along with the analytics services, Mixpanel also enables you to build applications integrated with it. In this post, we will work only with the Export API to export our data from Mixpanel.
As a web API, you can access it using tools like CURL or Postman or your favorite HTTP client for the language or framework of your choice. You can choose from the options given below:
- Apache HttpClient for Java
- Spray-client for Scala
- Hyper for Rust
- Ruby rest-client
- Python http-client
You can also 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 period of time
- 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 we want to export our raw data from Mixpanel. To do so, we’ll need to execute requests to the export endpoint. An example request to get raw events from Mixpanel is as follows:
SH
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:
SH
{"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 is 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 the instructions of this post.
Load Data from Mixpanel to SQL Data Warehouse
SQL Data Warehouse support numerous options for loading data, such as:
- PolyBase
- Azure Data Factory
- BCP command-line utility
- SQL Server integration services
To Load data from online services by using their exposed HTTP APIs, we will not consider the usage of BCP command-line utility or SQL server integration in this guide. Instead, we'll consider the case of loading our data as Azure storage Blobs and then use PolyBase to load the data into SQL Data Warehouse.
Accessing these services happens through HTTP APIs and APIs play an important role in both the extraction but also the loading of data into our data warehouse. You can access these APIs by using a tool like CURL or Postman or use the libraries provided by Microsoft for your favorite language. Before you actually upload any data you have to create a container which is something similar to a concept to the Amazon AWS Bucket, creating a container is a straightforward operation and you can do it by following the instructions found on the Blog storage documentation from Microsoft. As an example, the following code can create a container in Node.js.
SH
blobSvc.createContainerIfNotExists('mycontainer', function(error, result, response){ if(!error){ // Container exists and allows // anonymous read access to blob // content and metadata within this container } });
After the creation of the container you can start uploading data to it by using again the given SDK of your choice in a similar fashion:
SH
blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob', 'test.txt', function(error, result, response){ if(!error){ // file uploaded } });
When you are done putting your data into Azure Blobs you are ready to load it into SQL Data Warehouse using PolyBase. To do that you should follow the directions in the Load with PolyBase documentation. In summary, the required steps to do it, are the following:
- create a database master key
- create a database scoped credentials
- create an external file format
- create an external data source
PolyBase’s ability to transparently parallelize loads from Azure Blob Storage will make it the fastest tool for loading data. After configuring PolyBase, you can load data directly into your SQL Data Warehouse by simply creating an external table that points to your data in storage and then mapping that data to a new table within SQL Data Warehouse.
You will need to establish a recurrent process that will extract any newly created data from your service, load them in the form of Azure Blobs and initiate the PolyBase process for importing the data again into SQL Data Warehouse. One way of doing this is by using the Azure Data Factory service. In case you would like to follow this path you can read some good documentation on how to move data to and from Azure SQL Warehouse using Azure Data Factory.
The best way to load data from Mixpanel to SQL Data Warehouse and possible alternatives
So far we just scraped the surface of what can be done with Microsoft Azure SQL Data Warehouse 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 RudderStack that can handle this kind of problem automatically for you.
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 SQL Data Warehouse and start generating insights from your data. Don't want to go through the pain of direct integration? RudderStack’s Mixpanel to SQL Data Warehouse integration makes it easy to send data from Mixpanel to SQL Data Warehouse.