Mixpanel Community Icon

Tracking User Events on Your Server: A Guide for Client Access Issues

·
·

i have dont access my client code but i have mine server access client1.mydomain.com , client2.mydomain.com can i able to set the user events and track on our hosted server and how what i have to use // Example using Express.js const express = require('express'); const app = express(); const bodyParser = require('body-parser'); // Middleware to parse JSON requests app.use(bodyParser.json()); // Sample endpoint to track events app.post('/api/track-event', (req, res) => { const { userId, eventName, properties } = req.body; // Here you can log the event to your database or analytics service console.log('Event tracked:', { userId, eventName, properties }); // Respond to the client res.status(200).send({ success: true }); }); // Start the server app.listen(3000, () => { console.log('Server running on port 3000'); }); is this optimised solution?

  • Avatar of kapa.ai
    kapa.ai
    APP
    ·
    ·

    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:LBKSnpm install mixpanelLBKS2. Then, modify your Express.js server code to use Mixpanel:LBKSconst express = require('express'); const Mixpanel = require('mixpanel'); const bodyParser = require('body-parser');LBKSconst app = express(); app.use(bodyParser.json());LBKS// Initialize Mixpanel with your project token const mixpanel = Mixpanel.init('<YOUR_PROJECT_TOKEN> ');LBKS[app.post](http://app.post)('/api/track-event', (req, res) => { const { userId, eventName, properties } = req.body;LBKS // Track the event using Mixpanel mixpanel.track(eventName, { distinct_id: userId, ...properties, ip: req.ip // Include IP for geolocation });LBKS res.status(200).send({ success: true }); });LBKSapp.listen(3000, () => { console.log('Server running on port 3000'); });LBKSThis 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](http://client1.mydomain.com), [client2.mydomain.com](http://client2.mydomain.com)) through your central server, giving you more control over the tracking process and ensuring consistent data across platforms.

    kapa.ai is still learning and improving, please let me know how I did by reacting below.

    ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.

  • Avatar of PRASHANT P.
    PRASHANT P.
    ·
    ·
    app.post('/api/track-event', (req, res) => {
        const { userId, eventName, properties } = req.body;

    where i should to keep this api is?