Here at Variance, we are big fans of Zapier and Segment. As a heavy Zapier user, I have often wanted to push data from a Zap back to Segment so that it can be consumed by all the downstream systems (like Variance) that I have connected to a Segment source. Unfortunately, this is a bit more difficult to get right than I expected and so I figured it was worth sharing the solution (plus, I desperately want an easier place to go to when I am trying to remember how to do it).

But first, why?

Before we get into the “how,” let me quickly explain why I do this. One example is enrichment. We use a few different enrichment services and not all of them are supported super simply through Segment. So instead I run the enrichment through Zapier, grab all the data I want to include, and then fire an `identify` back up through Segment so that all the downstream systems get the new data.

Another example is marketing emails. If you manage your own unsubscribe center, you will generally need a good way to tell all your downstream systems a person has unsubscribed. The way I solve this is by catching a Webhook in Zapier when someone unsubscribes through our unsubscribe form and then sending both an `identify` call and a `track` event back to Segment so the downstream systems can be notified of what happens.

Alright, onto the show.

Step 1: Get your Segment write key and encode it

To get ourselves started we need to grab our Segment API key. To find this you’ll go to whatever source you want to send your `identify`/`track` calls through and navigate to Settings > API Key.

Grab your Write Key

From there you’ll copy the API key, which should look like a pretty random set of letters and numbers. It looks something like this: `BHCNFBzrW7uuGWjczXdm8f7RMbJj83` (don’t worry, that’s not my real key, I just generated it for this explainer).

Unfortunately, you can’t just stick that key into your Zapier Webhook and make it work. Here’s how Segment handles auth:

Authenticate to the Tracking API by sending your project’s Write Key along with a request. Authentication uses HTTP Basic Auth, which involves a `username:password` that is base64 encoded and prepended with the string `Basic`.

In practice that means taking a Segment source Write Key,`'abc123'`, as the username, adding a colon, and then the password field is left empty. After base64 encoding `'abc123:'` becomes `'YWJjMTIzOg=='`; and this is passed in the authorization header like so: `'Authorization: Basic YWJjMTIzOg=='`.

Luckily, base64 encoding is pretty easy. For this next step, you’ll want to open up your terminal and add this: `echo -n 'write_key:' | openssl base64` or, using my fake write key from earlier: `echo -n 'BHCNFBzrW7uuGWjczXdm8f7RMbJj83:' | openssl base64` (don’t forget the `:` after your Write Key). You should get back something like this: `QkhDTkZCenJXN3V1R1dqY3pYZG04ZjdSTWJKajgzOg==` (yours might look very different, or not have the == at the end, but if you did it right you should be good).

Open up your terminal

Sending your data out through Zapier

Now that we have our auth key, we can head over to Zapier and set up our outgoing Webhook. I’ll skip the steps before the Webhook, since you’ll be doing whatever you need, so let’s dig into sending data out.

To get ourselves started, we’ll choose the Webhooks by Zapier app and choose the POST option.

Great. Now we’re ready to set up our Webhook to Segment. You can send any Segment-supported data, but in this post we’ll go through sending an `identify` and `track` call. Let’s start with `identify`. As you likely already know, this is used to pass data (traits in Segment parlance) about a user. Here’s the basic setup:

URL: https://api.segment.io/v1/identify

Payload Type: json

Data: whatever you want to send as traits

Headers:`Authorization`:`Basic [Our base64 encoded write key from Step 1]`

Here’s how it looks in Zapier:

The track call looks almost the same, but requires a bit of extra data:

URL: https://api.segment.io/v1/track 
Payload Type: json
Data (key = value): 

  • `userId` = `user1234`
  • `event` = `Event Name`
  • `properties__property1` = `value1`
  • `properties__property2` = `value2`

Headers:`Authorization` = `Basic [Our base64 encoded write key from Step 1]`

Okay, a few big differences here. First, you need to include a `userId` and you need an `event` with the Event Name. The whole thing won’t work without that. (You could include an `anonymousId` if you wanted, but that’s not something I have done before.) Also, the way Zapier handles properties is that it nests anything that includes a `__` so `properties__property1` is actually `{‘properties’:{‘property1’}}`. That’s how we need to pass data in to get it recognized as a property on the track call.

Here’s what it all looks like: 

That’s it

You should be on your way!

If you use Segment and Zapier, you should really consider using Variance. Variance helps software teams grow their customers by tapping into all that data coming out of Segment and making it easy for revenue teams to access, work with, and notify on. If you’d like to give it a try, we offer free trials. If you have any questions about this post or Variance, please get in touch.

Try the Variance customer growth platform

Join some of the world's top software companies that use Variance to power data-driven sales.
Get Started 🎉

Subscribe to Variance News

An inside look at building a company delivered in (roughly) bi-monthly installments

Thanks for signing up.
Look out for our newsletter!
Oops! Something went wrong while submitting the form.

Get to know Variance

We believe the best way to learn about a product is to read the documentation. Have a look around.

Explore our Docs