App reviews, troubleshooting, and recommendations
Hi everyone,
I’m trying to send a pixel event to my theme extension using Server-Sent Events (SSE). The connection opens successfully, but no data streams are passing through.
Has anyone successfully implemented SSE in a Shopify theme extension to transmit events like this? I’d appreciate any guidance or examples on how to set up SSE correctly to send events for the pixel.
Thanks in advance for your help!
Hi @1563704123
Sure, I can help with that! Implementing Server-Sent Events (SSE) in a Shopify theme extension to send pixel events can be a bit tricky, especially if you’re not receiving the data streams as expected. Here’s a step-by-step breakdown to troubleshoot and set up SSE correctly:
First, you need to confirm that the server handling the pixel events is correctly configured to support SSE. Here's a simple example in Node.js to serve events:
const express = require('express');
const app = express();
app.get('/events', (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
// Example of sending events
setInterval(() => {
const data = JSON.stringify({ event: 'pixelEvent', value: 'User Interacted' });
res.write(`data: ${data}\n\n`);
}, 1000);
});
app.listen(3000, () => console.log('SSE server running on port 3000'));
Ensure your server sends proper Content-Type: text/event-stream and keeps the connection alive.
Inside your Shopify theme extension, you’ll need to establish an SSE connection. Here’s how you can do it in Javascript:
document.addEventListener('DOMContentLoaded', () => {
const eventSource = new EventSource('/events'); // Replace with your SSE endpoint
eventSource.onmessage = function (event) {
const data = JSON.parse(event.data);
console.log('Received pixel event:', data);
// Handle the pixel event
if (data.event === 'pixelEvent') {
sendPixelEventToAnalytics(data.value); // Replace with your pixel event logic
}
};
eventSource.onerror = function (error) {
console.error('SSE connection error:', error);
};
});
function sendPixelEventToAnalytics(value) {
// Example: send data to your analytics platform
console.log('Sending pixel event:', value);
}
Since Shopify has strict rules on how scripts interact within themes and extensions, make sure:
If your issue persists after following these steps, feel free to share your specific implementation or error details. We can dig deeper to get it resolved!
If you need any other assistance, feel free to reply and I will try my best to respond.
Best regards,
Daisy
Learn how to build powerful custom workflows in Shopify Flow with expert guidance from ...
By Jacqui May 7, 2025Did You Know? May is named after Maia, the Roman goddess of growth and flourishing! ...
By JasonH May 2, 2025Discover opportunities to improve SEO with new guidance available from Shopify’s growth...
By Jacqui May 1, 2025