How to load data from Intercom to Redshift
Extract your Data from Intercom
Intercom exposes a rich REST API for interacting with its services. There are two reasons someone would like to use the Intercom REST API:
- To pull data from it and use it for analytic purposes
- To push data into to enrich the information it contains for customers and enhance its service
In this post, we’ll focus mainly on pulling data from the API and use it to enrich our data warehouse with data that are generated from our interactions with our customers.
The main entities of the Intercom API are the following:
- Users – which is the primary way of interacting with Intercom.
- Leads – represent logged-out users of your application.
- Companies – allow you to represent commercial organizations using your product.
- Tags – A tag allows you to label your users and companies and list them using that tag.
- Segments – A segment is a group of your users defined by the rules that you set.
- Notes – Notes allow you to annotate and comment on your users.
- Events – Events are how you can submit user activity to Intercom.
- Counts – You can get counts of users and companies filtered by certain criteria.
- Conversations – Conversations are how you can communicate with users in Intercom.
At this point, it has to be noted that not all of the above entities can be pulled out from the Intercom API. For example, Events can only be pushed inside Intercom and it’s not possible to extract them again. So if you plan to use Intercom also for tracking the behavior of your users keep that in mind, because contrary to services like Mixpanel, it’s not possible to pull the user events from the system.
A good strategy for ensuring that your user activity will be pushed on all the different services that you need and that you will always have access to the data is by using a service like Segment.
Intercom exposes a RESTful web API, which means that we interact with its resources through HTTP verbs like POST and GET by using an HTTP client. Intercom also offers a number of SDKs that are built around an HTTP client for a number of popular languages:
Pull Data from the Intercom REST API
A typical use case for pulling data out of Intercom is to fetch all your users together with all the conversations you have done with each one. Then you can load this information to your data warehouse and enhance your analytic capabilities with additional interactions that you had with them. To do that you first need to get all your users, with CURL you can do that in the following way:
JSON
curl https://api.intercom.io/users-u pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709-H 'Accept: application/json'
A typical result will look like this:
JSON
{"type": "user.list","total_count": 105,"users": [{"type": "user","id": "530370b477ad7120001d",...},...],"pages": {"next": "https://api.intercom.io/users?per_page=50&page=2","page": 1,"per_page": 50,"total_pages": 3}}
Now we can also extract a full list of the conversations that have been performed on Intercom by doing the following:
JSON
$ curlhttps://api.intercom.io/conversations?type=admin&admin_id=25&open=true-u pi3243fa:da39a3ee5e6b4b0d3255bfef95601890afd80709-H 'Accept:application/json'
and a typical result will look like the following:
JSON
{"type": "conversation.list","conversations": [{"type": "conversation","id": "147","created_at": 1400850973,"updated_at": 1400857494,"user": {"type": "user","id": "536e564f316c83104c000020"},"assignee": {"type": "admin","id": "25"},"conversation_message": {"type": "conversation_message","subject": "","body": "<p>Hi Alice,</p>nn<p>We noticed you using our Product, do you have any questions?</p> n<p>- Jane</p>","author": {"type": "admin","id": "25"},"attachments": [{"name": "signature","url": "http://someurl.com/signature.jpg"}]}}]}
As we can see, each conversation contains a user object which contains an id, in this way we can associate the conversations with the users we had extracted earlier. Of course, to do that on our Data warehouse repository, we need to map the above structures to the data model that the repository follows by respecting both the schema and the data types.
Then we can write a pipeline that will extract the data and transform it into the model of our repository and load the data by following the instructions that follow below. Of course, if something changes on the Intercom API the pipeline will break and we will have to take care of it.
Use Webhooks to Push Events from Intercom to your Data Warehouse
Intercom also supports the definition of webhooks, where you can register certain events and the system will push there a message whenever the event is triggered. So, for example, you might define a webhook that will push a message every time a new conversation is performed with your customers.
By doing this it is possible to create a near real-time streaming load process for your data warehouse. To implement something like that though, you need to take into consideration the limitations of both ends, while ensuring the delivery semantics that your use case requires for the data management infrastructure that you will build.
For more information, you can check the webhooks section on the Intercom API documentation.
Prepare your Intercom 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 data types 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 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 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 data sources that are supported as input by Redshift, these are the following:
Load Data from Intercom to Redshift
The first step to load your Intercom 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 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 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 to data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:
- create a delivery stream
- add data to the stream
Whenever you add new data to the stream, Kinesis takes care of adding this 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 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.
JAVASCRIPT
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 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:
JAVASCRIPT
copy listingfrom '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.
The Best way to Load Data from Intercom to Redshift and Possible Alternatives
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 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 Intercom to Redshift and start generating insights from your data. Don't want to go through the pain of direct integration? RudderStack’s Intercom to Redshift integration makes it easy to send data from Intercom to Redshift.