Adding Klaviyo to your Gatsby project enables you to leverage powerful email marketing tools to engage with your audience.
If you haven't already, sign up for a Klaviyo account at Klaviyo. Klaviyo offers a free plan that includes basic features, which is sufficient for most small to medium-sized projects.
Once you've signed up and logged into your Klaviyo account, you need to create a public API key. This key will be used to identify your account and send data to Klaviyo.
To install Klaviyo's script on your Gatsby site, you'll need to add it to your site's <head> tag. This can be done by modifying the gatsby-ssr.js file.
import React from "react";
export const onRenderBody = ({ setHeadComponents }) => {
setHeadComponents([
<script
key="klaviyo"
type="text/javascript"
async
src={`https://static.klaviyo.com/onsite/js/klaviyo.js?company_id=YOUR_PUBLIC_API_KEY`}
/>,
]);
};
Replace YOUR_PUBLIC_API_KEY with the public API key you copied from Klaviyo.
Klaviyo can track various events on your website, such as page views, button clicks, and form submissions. To set up event tracking, use the newer Klaviyo API in your components.
For example, to track a button click event:
import React from "react";
const TrackButtonClick = () => {
const handleClick = () => {
window.KlaviyoAPI.track('Button Clicked', {
button_name: "Subscribe Button"
});
};
return (
<button onClick={handleClick}>
Subscribe
</button>
);
};
export default TrackButtonClick;
Integrating signup forms is crucial for building your email list. Klaviyo provides easy-to-use signup forms that you can customize and embed on your Gatsby site.
import React from "react";
const SignupForm = () => (
<div>
<!-- Copy and paste your Klaviyo embed code here -->
</div>
);
export default SignupForm;
After setting up Klaviyo on your Gatsby site, verify that the integration works correctly:
Integrating Klaviyo with your Gatsby project can significantly enhance your email marketing efforts. By following these steps, you can set up Klaviyo, track events, and embed signup forms on your site, allowing you to engage more effectively with your audience.