This guide provides a step-by-step process for automating the extraction, processing, and integration of app analytics data from Apple’s App Store Connect API into Mixpanel for analysis. We will go over how to set up JWT-based authentication, create and manage report requests, retrieve report segments, and finally parse and send data to Mixpanel.
Prerequisites
Before starting, make sure you have the following:
The App Store Connect API requires JWT (JSON Web Token) authentication, which involves creating a token using a .p8 key file and additional information from your Apple Developer account.
In your Apple Developer account:
To create the JWT, you’ll need the Key ID, Issuer ID, and Private Key from the .p8 file setup.
Import necessary libraries:
const fs = require('fs');
const jwt = require('jsonwebtoken');
const path = require('path');
const KEY_ID = 'YOUR_KEY_ID'; // Found in your developer account with the API key
const ISSUER_ID = 'YOUR_ISSUER_ID'; // Found in Users and Access in your developer account
const PATH_TO_KEY = path.join(__dirname, 'AuthKey.p8');
const privateKey = fs.readFileSync(PATH_TO_KEY, 'utf8');
javascript
Copy code
const token = jwt.sign(
{ iss: ISSUER_ID, exp: Math.floor(Date.now() / 1000) + (20 * 60), aud: 'appstoreconnect-v1' },
privateKey,
{ algorithm: 'ES256', header: { alg: 'ES256', kid: KEY_ID, typ: 'JWT' } }
);
The token variable now holds the JWT, ready for authentication with the App Store Connect API. (https://appstoreconnect.apple.com/access/integrations/api)
With the JWT token, we can create a report request to start retrieving app metrics. Apple allows reports to be configured as ONGOING (for continuous updates) or ONE_TIME_SNAPSHOT (for a single snapshot).
Specify the report type (ongoing or one-time) and include your app ID:
const requestData = {
data: {
type: 'analyticsReportRequests',
attributes: {
accessType: 'ONGOING', // or ONE_TIME_SNAPSHOT for a single snapshot
},
relationships: {
app: {
data: {
type: 'apps',
id: 'YOUR_APP_ID'
}
}
}
}
};
Make a POST request with the JWT token to initiate the report request:
async function createReportRequest() {
const url = 'https://api.appstoreconnect.apple.com/v1/analyticsReportRequests';
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestData)
};
try {
const response = await fetch(url, options);
const data = await response.json();
if (data.data && data.data.id) {
console.log(`Created report request ID: ${data.data.id}`);
}
} catch (error) {
console.error('Error creating report request:', error);
}
}
You can filter reports by specific categories (e.g., APP_USAGE, APP_STORE_ENGAGEMENT) and by name. This helps in customising data queries based on various parameters.
async function getReportByCategory(category, name) {
const url = `https://api.appstoreconnect.apple.com/v1/analyticsReportRequests?filter[category]=${category}&filter[name]=${name}`;
const headers = {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
};
try {
const response = await fetch(url, { method: 'GET', headers });
const data = await response.json();
console.log('Report Data:', data);
} catch (error) {
console.error('Error fetching report by category:', error);
}
}
After confirming the report request’s creation, retrieve specific instances of reports, which can include additional parameters to refine data by date, granularity, and other attributes.
async function getReportInstances(reportRequestId) {
const url = `https://api.appstoreconnect.apple.com/v1/analyticsReportRequests/${reportRequestId}/instances?filter[granularity]=DAILY&filter[processingDate]=YOUR_DATE`;
const headers = { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' };
try {
const response = await fetch(url, { method: 'GET', headers });
const data = await response.json();
console.log('Report Instances:', data);
} catch (error) {
console.error('Error fetching report instances:', error);
}
}
Once the report instances are available, each instance contains segments that give specific data points. Retrieve segments by accessing the segment URL provided in the report instance data.
async function getSegmentsForInstance(instanceId) {
const url = `https://api.appstoreconnect.apple.com/v1/analyticsReportInstances/${instanceId}/segments`;
const headers = { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' };
try {
const response = await fetch(url, { method: 'GET', headers });
const data = await response.json();
const segmentUrl = data.data[0].attributes.url; // Get the URL for downloading data
if (segmentUrl) {
downloadReportData(segmentUrl);
}
} catch (error) {
console.error('Error fetching segments:', error);
}
}
After retrieving the download URL from the segment data, download and decompress the data (often in .gz format).
async function downloadReportData(url) {
const dir = path.join(__dirname, 'reports');
const gzPath = path.join(dir, 'report.gz');
const csvPath = path.join(dir, 'report.csv');
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
const response = await fetch(url);
const fileStream = fs.createWriteStream(gzPath);
response.body.pipe(fileStream);
fileStream.on('finish', () => {
fs.createReadStream(gzPath)
.pipe(zlib.createGunzip())
.pipe(fs.createWriteStream(csvPath))
.on('finish', () => console.log('Report downloaded and extracted'));
});
}
With the CSV data available, parse it and send it to Mixpanel for tracking.
Parse the CSV Data:
fs.createReadStream('report.csv')
.pipe(csv())
.on('data', (row) => {
console.log('Parsed Row:', row);
// Add logic to structure data for Mixpanel
})
.on('end', () => {
console.log('CSV Parsing completed.');
});
Send Data to Mixpanel: Initialize Mixpanel and send parsed data:
npm install mixpanel
const mixpanel = require('mixpanel');
const mixpanelImporter = mixpanel.init('YOUR_PROJECT_TOKEN', {
secret: 'YOUR_SECRET_KEY',
host: 'api.mixpanel.com'
});
function sendToMixpanel(data) {
mixpanelImporter.track('Event Name', data, (err) => {
if (err) console.error('Error sending data to Mixpanel:', err);
else console.log('Data sent to Mixpanel');
});
}
Following this guide allows you to automate the entire process of fetching, processing, and sending Apple App Store data to Mixpanel for advanced insights. With this integration, you gain a streamlined way to track app performance and make data-driven decisions.
Refer to the Apple Store Connect API (https://developer.apple.com/documentation/appstoreconnectapi/downloading-analytics-reports) and Mixpanel API (https://developer.mixpanel.com/reference/overview) Documentation for further details.