Branch to Redshift

This page provides you with instructions on how to retrieve data from Branch and load it into Redshift. (If this manual process sounds like a lot, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

Collecting Branch Data

The first step for getting Branch data into Redshift is collecting that data from the Branch servers. This is possible using webhooks, a feature available to all Branch customers. Full documentation for Branch webhooks can be accessed hereWebhooks send data through user-defined HTTP POST callbacks. An application that uses Webhooks will POST data to a your custom endpoint when an event occurs.

If you follow the process laid out in the documentation, you will be able to retrieve the data you wish to load into Redshift. Useful endpoints like install, open, clicks, and web session start will be available to you via webhooks.

Sample Branch Data

When you starting seeing data coming through from Branch, it will be returned in JSON format.  Here is an example of what the JSON data looks like for the clicks endpoint:

User-agent: Branch Metrics API
Content-Type: application/json
    click_id: a unique identifier,
    event: 'click',
    event_timestamp: 'link click timestamp',
    os: 'iOS' | 'Android',
    os_version: 'the OS version',
    metadata: {
        ip: 'click IP',
        userAgent: 'click UA',
        browser: 'browser',
        browser_version: 'browser version',
        brand: 'phone brand',
        model: 'phone model',
        os: 'browser OS',
        os_version: 'OS version'
    query: { any query parameters appended to the link },
    link_data: { link data dictionary - see below }

// link data dictionary example
    branch_id: 'unique identifier for unique link',
    date_ms: 'link creation date with millisecond',
    date_sec: 'link creation date with second',
    date: 'link creation date',
    domain: 'domain label',
    data: {
        +url: the Branch link,
        ... other deep link data
    campaign: 'campaign label',
    feature: 'feature label',
    channel: 'channel label'
    tags: [tags array],
    stage: 'stage label',

Preparing Branch Data for Redshift

Now that you’ve got the desired data in JSON format, you need to map those data fields to a schema that can be inserted into your Redshift database. Consider each value in the webhook POST, identify a predefined data type (i.e. INTEGER, DATETIME, etc.) and build a table that can receive them.

The Branch documentation can help you define what fields and data types will be provided by each endpoint. Once you have identified all of the columns you will want to insert, use the CREATE TABLE statement in Redshift to build a table that will receive all of this data.

Inserting Branch Data into Redshift

It may seem like the easiest way to add your data is to build solid INSERT statements that add data to a Redshift table row-by-row. If you have been writing SQL for a while, you will be tempted to take this route. It will work, however it isn’t the most efficient way to go.

Redshift actually offers some good documentation for how to best insert data into new tables. The COPY command is particularly useful for this task, as it allows you to insert multiple rows without needing to build individual INSERT statements for each row.

If you cannot use COPY, it might help to use PREPARE to create a prepared INSERT statement, and then use EXECUTE as many times as required. This can help you avoid the overhead of constantly planning and parsing the INSERT statement.

Keeping Data Up-To-Date

Great! You’ve built a script that collects data from Branch webhooks and moves it into Redshift.  What about when your script doesn’t recognize a new data type, or when a record needs to be updated to a new value? The key is to build your script in such a way that it can also identify incremental updates to your data. 

Other Data Warehouse Options

Redshift is totally awesome, but sometimes you need to start smaller, or optimize for different things. In this case, many people choose to get started with Postgres, which is an open source RDBMS that uses nearly identical SQL syntax to Redshift. If you’re interested in seeing the relevant steps for loading this data into Postgres, check out Branch to Postgres

Easier and Faster Alternatives

If you have all the skills necessary to make  your way through this process, chances are building and maintaining a script like this isn’t a very good use of your time.

Products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts collecting your Branch data, structuring it in a way that is optimized for analysis, and inserting that data into your Redshift data warehouse.