Automate events tracking

Track user behaviour with Weespin API

Track user behavior and analytics in your application with Weespin's event tracking REST API to measure conversions, analyze engagement, and optimize your campaigns.

With the Weespin Events API, you can:

  • Track custom events (purchases, sign-ups, add to cart).
  • Associate events with users and generated links.
  • Collect valuable data to improve your product.

Get your API keys

  1. Log into your Weespin dashboard.
  2. Go to your application.
  3. Go to the Configuration section.
  4. In Integration:
  • Generate the API key that will be used to identify your application.

Understand the API documentation

Before coding, it's essential to know:

  • API base URL:
    API_BASE_URL
    https://developer.weespin.io
    
  • Main endpoints:
    POST /events/initialize → initialize device session

    POST /events → track user events
  • Integration workflow:
    1. Initialize device (once per session)
    2. Log events (with userId for all subsequent calls)

Initialize device

Before sending any events, you must initialize the device. This step is performed once at the start of the session and registers the device with the Weespin platform.

Endpoint: POST /initialize-device

Example request:

POST https://developer.weespin.io/initialize-device
Content-Type: application/json
Authorization: YOUR_APPLICATION_API_KEY

Response:


{
  "event": {
    "event_name": "app_installed",
    "event_type": "app_installed",
    "device_info": {
      "device_id": "ws_device_xxx",
      "platform": "Other",
      "os": "Linux"
    },
    "location": {
      "country": "US",
      "city": "",
      "latitude": "0",
      "longitude": "00.0"
    }
  }
  
}

Log events

Once the device is initialized, you can track user actions by sending events.

Endpoint: POST /events

Required parameters:

event_namestring
Name of the event (e.g: purchase, add_to_cart)
device_idstring
The unique id of the event returned by the initialize endpoint

Other parameters:

payloadobject
Custom event data with contextual information

Example request:

POST https://developer.weespin.io/events
Content-Type: application/json
Authorization: YOUR_APPLICATION_API_KEY

{
  "event_name": "PURCHASE",
  "device_id": "ws_device_xxx",
  "payload": {
    "Product": "Trending shoe",
    "Qty": 2,
    "Amount": 2500
  }
}

Response:


Payload rules:

  • event_name must be a non-empty string.
  • payload must be a JSON-compatible object.
  • Supported types: string, int, double, boolean

Common event names:

  • add_to_cart - User adds a product to cart
  • purchase - User completes a transaction
  • signup - New user registration
  • login - User authentication
  • share_link - User shares content
  • open_app - App launch

Practical implementation examples

const WEESPIN_API_KEY = process.env.WEESPIN_API_KEY;
const BASE_URL = 'https://developer.weespin.io';

// Step 1: Initialize device (once per session)
async function initializeDevice(deviceId, platform) {
  try {
    const response = await fetch(`${BASE_URL}/initialize-device`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `${WEESPIN_API_KEY}`
      },
    });

    const data = await response.json();
    console.log('Device initialized:', data.device_id);
    return data;
  } catch (error) {
    console.error('Error initializing device:', error.message);
  }
}

// Step 2: Log event using device_id
async function logEvent(eventName, deviceId, payload) {
  try {
    const response = await fetch(`${BASE_URL}/events`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `${WEESPIN_API_KEY}`
      },
      body: JSON.stringify({
        event_name: eventName,
        device_id: deviceId,
        payload: payload
      })
    });

    const data = await response.json();
    console.log('Event logged:', data.event.event_name);
    return data;
  } catch (error) {
    console.error('Error logging event:', error.message);
  }
}

// Usage
const deviceId = 'ws_device_xxx';

initializeDevice(deviceId, 'web');
logEvent('purchase', deviceId, {
  product: 'Trending shoe',
  qty: 2,
  amount: 2500
});

Conclusion

You now have the tools to integrate event tracking into your application! Use the Weespin Events API to track user behavior, measure conversions, and optimize your product based on real data.