Back to all functions

Mixpanel: Track Event

Function to create tracking of a particular event in Mixpanel

Created By
Muhammad Dabeer
Community
download-icon
INPUT VARIABLES
{
insertId
}
A unique identifier for the event, used for deduplication. Events with identical values for (event, time, distinct_id, $insert_id) are considered duplicates; only the latest ingested one will be considered in queries.
{
eventProperties
}
other event properties
{
distinctId
}
The unique identifier of the user who performed the event.
{
insertId
}
A unique identifier for the event, used for deduplication. Events with identical values for (event, time, distinct_id, $insert_id) are considered duplicates; only the latest ingested one will be considered in queries.
{
projectToken
}
Project token
{
time
}
The time at which the event occurred, in seconds or milliseconds since UTC epoch.
share-icon
OUTPUT VARIABLES
{
response
}
API response message
{
}
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
success
}
{
error
}
{
}
{
}
{
}
{
}

Function Code Snippet

 
export default async function main(args) {
  let { event, projectToken, distinctId, time, insertId, eventProperties } = args.inputVars; // Input variables

  // Check if the user has inputted the required variables
  if (!projectToken || !distinctId || !time || !insertId) {
    return {
      // Returns the error path so we can continue the design
      next: { path: 'error' },
      // Renders a debug message in Voiceflow
      trace: [{ type: "debug", payload: { message: "Missing required input variables for this function" } }]
    };
  }

  // Base URL for sending data to Mixpanel
  const url = 'https://api.mixpanel.com/track?verbose=1';

  // Create the data payload
  const payload = [
    {
      event: event,
      properties: {
        token: projectToken,
        time: time,
        distinct_id: distinctId,
        $insert_id: insertId,
        ...eventProperties
      }
    }
  ];

  // Setup the request options, including headers
  const config = {
    method: 'POST',
    headers: {
      'Accept': 'text/plain',
      'Content-Type': 'application/json',
      'Connection': 'keep-alive'
    },
    body: JSON.stringify(payload)
  };

  // This is where we made the fetch request, we use try-catch for error handling
  try {
    // Make the fetch request
    const response = await fetch(url, config);

    // Check if the response status is OK (status in the range 200-299)
    if (!response.ok) {
      // If not OK, throw an error to be caught by the catch block
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    // Parse the response
    const responseBody = await response.json;
    if (responseBody.error) {
     return {
          // Maps the error path so we can continue in our design
          next: { path: 'error' },
          // Renders a debug message in Voiceflow with the error
          trace: [{ type: "debug", payload: { message: "Error:" + responseBody.error } }]
        };
    }

    // Create the return objects if this is successful
    return {
      // Map the success path so we can continue in our flow
      next: { path: 'success' },
      // Optionally, return the response body if needed
      outputVars: {
        response: responseBody.status  //JSON.stringify(responseBody)   //"{"error":null,"status":1}"
      }
    };
  }
  // Catches all the errors we threw and displays the debug message
  catch (error) {
    return {
      // Maps the error path so we can continue in our design
      next: { path: 'error' },
      // Renders a debug message in Voiceflow with the error
      trace: [{ type: "debug", payload: { message: "Error:" + error.message + ' ' + url } }]
    };
  }
}
copy-icon

Function Walkthrough

Explore More Functions

Build and submit a Function to have it featured in the community.

ghraphic