Using the Mixpanel API to Get Profiles (with PHP examples)

by Endgrate Team 2024-08-18 5 min read

Mixpanel homepage

Introduction to Mixpanel API Integration

Mixpanel is a powerful analytics platform that helps businesses track user interactions and gain insights into customer behavior. With its robust set of tools, Mixpanel allows companies to optimize their products and marketing strategies by providing detailed data analysis and visualization capabilities.

Developers often integrate with Mixpanel's API to access user profile data, enabling them to create personalized user experiences and targeted marketing campaigns. For example, a developer might use the Mixpanel API to retrieve user profiles and segment them based on specific behaviors or attributes, allowing for more precise targeting in marketing efforts.

Setting Up Your Mixpanel Test Account

Before you can start using the Mixpanel API to retrieve user profiles, you'll need to set up a Mixpanel account. This involves creating a service account, which is essential for authenticating your API requests. Follow these steps to get started:

Create a Mixpanel Account

If you don't already have a Mixpanel account, visit the Mixpanel website and sign up for a free account. This will give you access to Mixpanel's analytics tools and allow you to create projects for testing purposes.

Generate a Service Account for API Access

Mixpanel uses service accounts for API authentication. A service account is a special type of user account intended for non-human entities like scripts or backend services. Here's how to create one:

  1. Log in to your Mixpanel account and navigate to the Organization Settings.
  2. Go to the Service Accounts tab.
  3. Click on Create Service Account.
  4. Select the appropriate role and projects for the service account.
  5. Once created, make sure to securely store the service account's username and secret, as they will be used for API authentication.

For more details, refer to the Mixpanel Service Accounts documentation.

Authenticate Using HTTP Basic Auth

Mixpanel's API requires HTTP Basic Auth for authentication. You will need the service account's username and secret. Here's an example of how to authenticate using PHP:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mixpanel.com/api/app/me");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, ":");
$response = curl_exec($ch);
curl_close($ch);

Replace <serviceaccount_username> and <serviceaccount_secret> with your actual service account credentials.

Once your service account is set up and authenticated, you can proceed to make API calls to retrieve user profiles and other data from Mixpanel.

Mixpanel authentication documentation page.
sbb-itb-96038d7

Making API Calls to Retrieve User Profiles from Mixpanel Using PHP

Now that you have set up your Mixpanel account and authenticated using a service account, it's time to make API calls to retrieve user profiles. This section will guide you through the process of using PHP to interact with Mixpanel's API.

Prerequisites for Using PHP with Mixpanel API

Before proceeding, ensure you have the following installed on your machine:

  • PHP 7.4 or later
  • cURL extension for PHP

These tools will allow you to make HTTP requests to Mixpanel's API and handle the responses effectively.

Installing Required PHP Dependencies

Ensure that the cURL extension is enabled in your PHP installation. You can check this by looking for extension=curl in your php.ini file. If it's not enabled, uncomment the line or add it if necessary.

Example Code to Query Mixpanel User Profiles

Below is a PHP example demonstrating how to query user profiles from Mixpanel using the Engage API:


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mixpanel.com/api/query/engage");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, ":");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "where" => "properties[\"$email\"] == \"user@example.com\""
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
foreach ($data['results'] as $profile) {
    echo "User ID: " . $profile['$distinct_id'] . "\n";
    echo "Email: " . $profile['properties']['$email'] . "\n";
}

Replace <serviceaccount_username> and <serviceaccount_secret> with your actual service account credentials. The example above queries profiles where the email matches "user@example.com".

Understanding the API Response

The response from Mixpanel's API will include user profiles that match the specified criteria. You can iterate through the results and access various properties of each user profile, such as their distinct ID and email address.

Handling Errors and Verifying API Requests

It's crucial to handle potential errors when making API requests. Check the HTTP status code in the response to ensure the request was successful. A status code of 200 indicates success, while other codes may indicate errors. Refer to Mixpanel's documentation for specific error codes and their meanings.

To verify that your request succeeded, you can log the response or check the Mixpanel dashboard to see if the queried profiles match the expected results.

For more detailed information on querying profiles, refer to the Mixpanel Engage Query documentation.

Mixpanel API call documentation page.

Conclusion and Best Practices for Using Mixpanel API with PHP

Integrating with the Mixpanel API using PHP allows developers to efficiently access and manage user profile data, enabling personalized user experiences and targeted marketing strategies. By following the steps outlined in this guide, you can set up a service account, authenticate your requests, and retrieve user profiles with ease.

Best Practices for Secure and Efficient Mixpanel API Integration

  • Secure Storage of Credentials: Always store your service account credentials securely. Consider using environment variables or secure vaults to prevent unauthorized access.
  • Handle Rate Limits: Mixpanel enforces rate limits to ensure system integrity. The Query API allows a maximum of 60 queries per hour and 5 concurrent queries. Plan your API calls accordingly to avoid hitting these limits. For more details, refer to the Mixpanel Rate Limits documentation.
  • Error Handling: Implement robust error handling to manage API response codes effectively. Log errors and handle exceptions to ensure smooth operation.
  • Data Transformation: Standardize and transform data fields as needed to maintain consistency across your application.

Streamlining Integrations with Endgrate

While integrating with Mixpanel is powerful, managing multiple integrations can be complex and time-consuming. Endgrate simplifies this process by providing a unified API endpoint that connects to various platforms, including Mixpanel. By using Endgrate, you can save time and resources, allowing you to focus on your core product development.

Explore how Endgrate can enhance your integration strategy by visiting Endgrate's website and discover a more efficient way to manage your integrations.

Read More

Ready to get started?

Book a demo now

Book Demo