Back

Quick Guide to Implementing Webhooks in Fathom

Aug 15, 20246 minute read

Hey there, fellow JavaScript aficionado! Ready to supercharge your Fathom integration with some webhook magic? Let's dive right in and get those real-time updates flowing.

Prerequisites

Before we start, make sure you've got:

  • A Fathom account with API access
  • Node.js installed and ready to roll
  • Your favorite code editor at hand

If you're good to go, let's jump into the fun stuff!

Setting Up Webhooks with Fathom API

First things first, we need to authenticate with the Fathom API. It's pretty straightforward:

const axios = require('axios'); const fathomApi = axios.create({ baseURL: 'https://api.fathom.com/v1', headers: { 'Authorization': `Bearer YOUR_API_KEY_HERE` } });

Replace YOUR_API_KEY_HERE with your actual API key, and you're set!

Creating a Webhook

Now, let's create a webhook. Here's how you do it:

async function createWebhook() { try { const response = await fathomApi.post('/webhooks', { url: 'https://your-app.com/webhook', events: ['pageview', 'goal_complete'] }); console.log('Webhook created:', response.data); } catch (error) { console.error('Error creating webhook:', error.response.data); } } createWebhook();

Easy peasy, right? This sets up a webhook that'll ping your app whenever there's a pageview or a goal completion.

Configuring Webhook Events

Fathom offers a bunch of event types you can listen to. We used 'pageview' and 'goal_complete' in the example above, but there are more:

  • pageview
  • goal_complete
  • campaign_view
  • campaign_conversion

Mix and match these in the events array to get exactly what you need.

Handling Webhook Payloads

When Fathom sends a webhook, you'll get a juicy payload. Here's what it might look like:

{ "event": "pageview", "timestamp": "2023-06-15T14:30:00Z", "data": { "site_id": "ABCD1234", "page_url": "https://example.com/awesome-page", "referrer": "https://google.com", "unique_visitor": true } }

To handle this in your app, you might do something like:

app.post('/webhook', (req, res) => { const { event, data } = req.body; if (event === 'pageview') { // Do something with the pageview data console.log(`New pageview on ${data.page_url}`); } res.sendStatus(200); });

Securing Webhooks

Security is crucial, folks! Fathom signs each webhook with a secret. Here's how you can verify it:

const crypto = require('crypto'); function verifyWebhookSignature(payload, signature, secret) { const hmac = crypto.createHmac('sha256', secret); const digest = hmac.update(payload).digest('hex'); return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(digest)); } app.post('/webhook', (req, res) => { const signature = req.headers['x-fathom-signature']; const isValid = verifyWebhookSignature(JSON.stringify(req.body), signature, 'your_webhook_secret'); if (!isValid) { return res.status(401).send('Invalid signature'); } // Process the webhook... });

Testing and Debugging

Fathom provides some nifty tools for testing your webhooks. Use them! They're a lifesaver when you're trying to figure out why that darn webhook isn't firing.

If you're stuck, double-check your API key, webhook URL, and event types. And don't forget to check your server logs!

Best Practices

A few pro tips to keep in mind:

  • Always handle errors gracefully
  • Implement retry logic for failed webhook deliveries
  • Keep an eye on your webhook performance as you scale

Wrapping Up

And there you have it! You're now armed with the knowledge to implement webhooks in Fathom like a pro. Remember, practice makes perfect, so don't be afraid to experiment and iterate.

Got questions? Hit up the Fathom API docs or jump into the community forums. Happy coding, and may your webhooks always fire true!