Yes, you can track user events on your hosted server using Mixpanel's server-side tracking. This is actually recommended by Mixpanel for more reliable and accurate data collection. Here's how you can implement this using Mixpanel's Node.js SDK:
1. First, install the Mixpanel Node.js library using npm:
```
npm install mixpanel
LBKS2. Then, modify your Express.js server code to use Mixpanel:
LBKS```
const express = require('express');
const Mixpanel = require('mixpanel');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
// Initialize Mixpanel with your project token
const mixpanel = Mixpanel.init('<YOUR_PROJECT_TOKEN> ');
[app.post](http://app.post)('/api/track-event', (req, res) => {
const { userId, eventName, properties } = req.body;
// Track the event using Mixpanel
mixpanel.track(eventName, {
distinct_id: userId,
...properties,
ip: req.ip // Include IP for geolocation
});
res.status(200).send({ success: true });
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
This solution is optimized for server-side tracking with Mixpanel. Here's why it's a good approach:
1. Server-side tracking is more reliable and not susceptible to ad-blockers, as recommended by Mixpanel Server-Side Tracking.
2. It uses the official Mixpanel Node.js SDK, which provides built-in queueing, retrying, and batching capabilities Mixpanel SDKs: Node.js.
3. The distinct_id is set to the userId, which is important for user identification Server-side Identity Management.
4. It includes the ip property, which is crucial for accurate geolocation in server-side implementations Sending Events.
Remember to replace <YOUR_PROJECT_TOKEN> with your actual Mixpanel project token.
This approach allows you to track events from multiple client domains (client1.mydomain.com, client2.mydomain.com) through your central server, giving you more control over the tracking process and ensuring consistent data across platforms.