json-object-editor
Version:
JOE the Json Object Editor | Platform Edition
79 lines (65 loc) • 2.71 kB
JavaScript
function EngagementTracker() {
const self = this;
// 📸 Email open pixel endpoint
// Called as: /API/plugin/engagementTracker/pixel?campaign=...&visitor=...
this.pixel = async function(data, req, res) {
const campaign_id = req.query.campaign;
const visitor = req.query.visitor;
if (!campaign_id) return res.status(400).send({ error: "Missing campaign ID" });
const engagement = {
_id: cuid(),
itemtype: "engagement_event",
eventType: "email_open",
eventTarget: "email",
eventLabel: "Email opened",
campaign: campaign_id,
visitor: visitor || "anonymous",
eventLocation: "email",
created: new Date()
};
await JOE.Storage.save(engagement, "engagement_event");
// Transparent 1x1 gif
const img = Buffer.from("R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==", "base64");
res.writeHead(200, {
"Content-Type": "image/gif",
"Content-Length": img.length
});
return res.end(img);
};
// 🔗 Click-through tracking + redirect
// Called as: /API/plugin/engagementTracker/redirect?campaign=...&to=URL&target=cta_1&visitor=...
this.redirect = async function(data, req, res) {
const { campaign, to, target, visitor } = req.query;
if (!to || !campaign) return res.status(400).send({ error: "Missing required parameters" });
const engagement = {
_id: cuid(),
itemtype: "engagement_event",
eventType: "click_cta",
eventTarget: target || "unspecified",
eventLabel: `Clicked ${target || "link"}`,
campaign,
visitor: visitor || "anonymous",
eventLocation: "email_or_landing",
created: new Date()
};
await JOE.Storage.save(engagement, "engagement_event");
return res.redirect(to);
};
// ✍️ Generic event logger
// Called via POST to /API/plugin/engagementTracker/log with JSON body
this.log = async function(data, req, res) {
if (!data || !data.eventType || !data.campaign) {
return res.status(400).send({ error: "Missing eventType or campaign" });
}
const engagement = {
...data,
_id: cuid(),
itemtype: "engagement_event",
created: new Date()
};
await JOE.Storage.save(engagement, "engagement_event");
return res.send({ status: "ok" });
};
return self;
}
module.exports = new EngagementTracker();