
Importing via Warehouse Connectors
If your ad spend data is already in a data warehouse, you can bring it into Mixpanel using Warehouse Connectors — no custom code or cloud functions required.Using the Ad Spend Template
Link to Demo The Ad Spend template provides a streamlined setup for mapping your warehouse table to Mixpanel’s ad spend data model.- Connect your warehouse in Project Settings → Warehouse Sources.
- Select a table or view that contains your ad metrics and choose the Ad Spend template.
- Map columns for required Mixpanel fields (event name, timestamp) and ad metrics like
cost,clicks, andimpressions. - Set a sync schedule (e.g. hourly or daily). Mixpanel will sync rows from your table and turn each one into an event you can analyze in Insights and other reports.
Importing via APIs
The sections below walk through building custom API pipelines to pull data directly from ad networks and send it to Mixpanel. This approach gives you full control but requires writing and hosting code.Understanding the Data Model
Events are the core of Mixpanel’s data model. Fundamentally, an event is a row of data with a name, a timestamp, and a set of properties. This is how we can represent Ad Data as events.- Only include base metrics cost, clicks, and impressions. We don’t need to send derived metrics like Cost-per-click, because Mixpanel’s Custom Properties and Formulas allow us to calculate and alter derived metrics on the fly.
- Matching client side properties: if you are using a Mixpanel client-side SDK to track user behaviors, you will want to model
additional campaign metadata (source, medium, campaign, etc…) as
utm_source,utm_mediumetc… this matches the way mixpanel’s SDKs capture UTM params by default - No Distinct ID: You’ll notice that our event has no Distinct ID. This is because ad performance data isn’t tied to any particular user. This is the key difference from behavioral events. By omitting it, we are ensuring that these events do not get erroneously included in reporting that intend to analyze user behavior such as Funnels, Retentions, Flows, unique user counts, “did not do” cohorts, etc.
- Event properties are aggregated: You’ll notice the Ad-Data event in this example is scheduled to trigger only once a day. Properties are aggregated counts of all clicks through the day, all impressions through the day, all ad-spend through the day. Reason for this is ad-networks only export data at an aggregate level (without user details) and at fixed intervals (lowest granularity is generally a day)
- Include an Insert ID: It’s recommended to include the Insert ID property for these kinds of events. This allows you to send the campaign data to Mixpanel more than once for a particular segment without duplicating the data in reports.
- The ad network name
- The date of the performance data
- The campaign ID
Gathering Data from Ad Networks
Different ad networks provide different ways to access their raw data. Some allow you to manually download a CSV export that can be transformed into the above format and sent to Mixpanel. Most networks also have APIs that allow you to automate exporting the metrics that you’re interested in.💡 Note: Some of these APIs require registration and permissions with the underlying platform, please read their docs to set this up.
- Google Ads provides a Reporting API that can be used to extract performance data: https://developers.google.com/google-ads/api/docs/reporting/overview
- Facebook provides a Marketing Insights API for ad performance metrics: https://developers.facebook.com/docs/marketing-api/insights
- LinkedIn provides a Marketing Reporting API for ad performance data: https://learn.microsoft.com/en-us/linkedin/marketing/integrations/ads-reporting/getting-started
- Twitter provides an Ads Analytics API that can be used to extract performance data: https://developer.twitter.com/en/docs/twitter-ads-api/analytics/overview
Detailed Guides
Below are detailed guides for Google and Facebook Ad Networks. The same general process described in these two guides can be followed for any other ad network as well by using their export APIs and pushing the data to Mixpanel.Prerequisites
In order to follow this guide, you will need:- Mixpanel project token (can be found in Mixpanel project settings)
- Mixpanel secret token (can be found in Mixpanel project settings)
- This is required to track back-dated events
- Google Ads Developer Token
- An account with Google Ads access to your ad campaigns
- A Google Cloud Platform project
Accessing Google Ads API
Our Google Cloud Function will use the Google Ads API to extract relevant campaign performance data for the previous day. To access their API, we will need to create a new Google API Credential that has access to the Google Ads API.Creating your Credentials
- In your Google Cloud Platform project, navigate to the Credentials screen
- From the Create credentials dropdown, create a new OAuth client ID
- For Application type, select Web application
- Add an Authorized redirect URIs entry for
https://developers.google.com/oauthplayground- This is only for the next step and can be removed afterwards
- Create the credential
Enabling Google Ads API access
We need to allow our new credential to access the Google Ads API by enabling it.- Navigate to the Enabled APIs & services page in Google Cloud
- Click Enable APIs and services
- Find Google Ads API, and press Enable
Generating a Refresh Token
The credential you created allows Google to track access to the Google Ads API, but it doesn’t specify which Google user its acting on behalf of to retrieve the correct ad performance data. We will need to give the credential access to your Google Ads account so that it can export metrics on your behalf.- Go to the Google OAuth Playground
- Click the Settings cog in the upper right corner
- Set OAuth flow to Server-side
- For Access type, select Offline
- This will ensure we get a refresh token, and not just a short-lived access token
- Check the Use your own OAuth credentials checkbox
- Fill in the Client ID and Client Secret from the credential that you created earlier
- In the left hand side, find Google Ads API, and select it
Get the customer client ID
The last piece of information we will need is the Client Customer ID. It is the ID of your Google Ads user account, which we will need to know where to pull campaign data from. Log into Google Ads, and note the numbers at the top right of the screen under your display name.
123-456-7890.
Creating the function
Now that we have all of the information we need, we can create our Google Cloud Function. The function will run once a day, export the previous day’s campaign metrics, transform them into Mixpanel events, and import them our Mixpanel project.- Go to the Google Cloud Functions page
- Click Create function
- In the Environment dropdown, select 2nd gen
- For Function name, choose something informative like
mixpanel-ads-google - Open Runtime, build, connections and security settings and increase the Memory allocated and CPUs from the defaults
- Depending on your ad network volume, you might want to play with these settings to process all of your data. You can always change these after creation.
- Press Next
- Select Node.js 18 as the Runtime
- For Entry point, set it to
handler
💡 Google doesn’t provide a first-party Ads API client for Node.js. The one we are using is third-party. More information can be found here.For the function’s code, select index.js in the file tree and replace its contents with this snippet:
Schedule your function
We want the function to run by itself every day so we can regularly inspect the data in Mixpanel. We’ll do this by using Google Cloud Scheduler to run our Google Cloud Function every morning at 8 AM UTC.- Navigate to the Google Cloud Schedule page in your project
- Click Create job at the top of the page
- For Name, choose something descriptive such as
gcf-mixpanel-ads-google-daily - In Frequency, put
0 8 * * *- This is a unix-cron expression that means to run every day at the 8th hour (8:00 AM)
- Google Ads has a short delay before metrics are available. Choosing a time sufficiently later than midnight ensures that the data will be ready for us to retrieve.
- Select Coordinated Universal Time (UTC) as the Timezone
- This can also be your local time if you prefer
- Press Continue
- Select HTTP as the Target type
- In URL, paste the URL that you got from your Cloud Function
- Set the HTTP method to GET
- In the Auth header dropdown, choose Add OIDC token
- Select the service account that has access to your Google Cloud Function in the Service account dropdown
- This gives Cloud Scheduler the access required to invoke your function on your behalf
- Press Create to create your new schedule
Wrapping up
That’s it! We’ve made a Google Cloud Function that syncs Google Ads data to Mixpanel every morning.Prerequisites
We’ll be using Facebook’s Marketing API to retrieve ad data from your ads account. In order to follow this guide, you will need:- Mixpanel project token (can be found in Mixpanel project settings)
- Mixpanel secret token (can be found in Mixpanel project settings)
- This is required to track back-dated events
- Facebook App Access Token with the
ads_readpermission - Facebook Ad Account ID
- You can get this from the Ad account dropdown at the top of Facebook Ads Manager
- A Google Cloud Platform project
Creating the function
The function will run once a day, export the previous day’s campaign metrics, transform them into Mixpanel events, and import them our Mixpanel project.- Go to the Google Cloud Functions page
- Click Create function
- In the Environment dropdown, select 2nd gen
- For Function name, choose something informative like
mixpanel-ads-facebook - Open Runtime, build, connections and security settings and increase the Memory allocated and CPUs from the defaults
- Depending on your ad network volume, you might want to play with these settings to process all of your data. You can always change these after creation.
- Press Next
- Select Node.js 18 as the Runtime
- For Entry point, set it to
handler
Schedule your function
We want the function to run by itself every day so we can regularly inspect the data in Mixpanel. We’ll do this by using Google Cloud Scheduler to run our Google Cloud Function every morning at 8 AM UTC.- Navigate to the Google Cloud Schedule page in your project
- Click Create job at the top of the page
- For Name, choose something descriptive such as
gcf-mixpanel-ads-facebook-daily - In Frequency, put
0 8 * * *- This is a unix-cron expression that means to run every day at the 8th hour (8:00 AM)
- Some ad networks may have a delay before metrics are available. Choosing a time sufficiently later than midnight ensures that the data will be ready for us to retrieve.
- Select Coordinated Universal Time (UTC) as the Timezone
- This can also be your local time if you prefer
- Press Continue
- Select HTTP as the Target type
- In URL, paste the URL that you got from your Cloud Function
- Set the HTTP method to GET
- In the Auth header dropdown, choose Add OIDC token
- Select the service account that has access to your Google Cloud Function in the Service account dropdown
- This gives Cloud Scheduler the access required to invoke your function on your behalf
- Press Create to create your new schedule