Published onĀ 

Sending Plunk emails through Segment Functions

Segment empowers business around the world to collect, clean and automate customer data. Segment is a powerful tool so it only makes sense for us to show you how to integrate Plunk with Segment.

In this tutorial you will learn how to send Plunk emails through Segment Functions.

Sending Plunk emails through Segment Functions

What are Segment Functions?

If you are not familiar with Segment Functions, they are a powerful tool that allows you to extend Segment's functionality. You can use Functions to send emails, make API calls, or even run machine learning models. You can read more about Segment Functions here.

We will be using Functions to pass on events from Segment to Plunk, so we can easily send marketing emails to our customers.

Getting started

Setting up a source

Everything in Segment starts from a source, so let's create one. A source can be anything from a website you own to a third-party app you are using. It does not matter which one you use, as long as you can send events and identify users.

It is important that you also attach the email address of the user to the event. Just like in the Slack example, you need to send the email address of the user in the event, along with the event.

Creating a new function

In your Segment Workspace, head over to the Connections tab and find the catalog. In there you can find loads of pre-built integrations. It is also the spot where you can create new Functions.

An image showing the option to create a new Function in Segment

Since we want to send events to Plunk, we will be using a destination function. Destination functions take in a Segment event, process it and send it to a new destination.

When you create a new function, Segment already provides you with a template. This template is exactly what we are looking for so we just need to make some small changes!

Selecting our event

To start off, we will select the event we want to send to Plunk. It makes it easier to write our function and test it since Segment hints you with the event properties. In my case, I am sending a user's email address and a project name but feel free to send whatever floats your boat.

An image showing the custom event selector in Segment Functions

Adding our Plunk API key

Before we can send events to Plunk, we need to add our API key to the function. You do this in the Settings tab of the function. Add a new variable called apiKey and make it sensitive.

An image showing the option to create a new Function in Segment

Updating the endpoint

Somewhere in the function you will find a variable called endpoint, this is the one we are looking for! Replace it with Plunk's API endpoint.

1const endpoint = '';

Changing our API request

The last thing we need to do is update the actual request part. No worries, it is not as scary as it looks! We have written a couple solid lines of code that you can copy and paste into your function.

Copy everything from the default template and replace it with our example. It is not much different from the example but slightly better!

2 * Handle track event
3 * @param  {SegmentTrackEvent} event
4 * @param  {FunctionSettings} settings
5 */
6async function onTrack(event, settings) {
7	const endpoint = '';
8	let response;
10  if (! {
11    throw new InvalidEventPayload("No email found in the properties")
12  }
14	try {
15		response = await fetch(endpoint, {
16			method: 'POST',
17			headers: {
18				Authorization: `Bearer ${settings.apiKey}`,
19				'Content-Type': 'application/json'
20			},
21			body: JSON.stringify({
22				event: event.event,
23				email:
24			})
25		});
26	} catch (error) {
27		// Retry on connection error
28		throw new RetryError(error.message);
29	}
31	if (response.status >= 500 || response.status === 429) {
32		throw new RetryError(`Failed with ${response.status}`);
33	}

Linking it all together

Now that we have our source and destination function, all that is left to do is link the two together. Segment will automatically guide you through this process and since Plunk only requires your API key there really is only one field left to fill out!

An image showing an active Function destination in Segment

In Summary

As you can see, you can easily send events to Plunk using Segment Functions. It is a very accessible way to get started with Plunk and requires very little effort from your side!