Pull modeled metrics for a date range
Steps and code that copy each modeled metric row of a date range from the Prescient GraphQL API, one page at a time.
To copy the modeled metrics of a date range, send the modeledMetrics query, then send it again with the cursor of each response until no page is left. One response has 5,000 rows at most.
How do you pull the rows?
Section titled “How do you pull the rows?”-
Choose the date range.
startDateandendDateuse theYYYY-MM-DDformat, and both dates are in the range. If you leave them out, the range is the last 30 days. -
Choose the fields that you need. Select
pageInfoin each request, because it has the cursor.Query query ModeledMetrics($startDate: ISO8601Date$endDate: ISO8601Date$after: BigInt) {modeledMetrics(startDate: $startDate, endDate: $endDate, after: $after) {data {reportedDatesourceChannelNamesourceCampaignIdtargetmetricNamemetricValue}pageInfo {endCursorhasNextPage}}} -
Send the first request with no
aftervalue. -
If
pageInfo.hasNextPageistrue, send the same query withafterset topageInfo.endCursor. Keep each other variable the same. -
Stop when
hasNextPageisfalse.
What does the full loop look like?
Section titled “What does the full loop look like?”Save the query of step 2 as a string, then run the loop. In each example, the key comes from the PRESCIENT_API_KEY environment variable.
const QUERY = ` query ModeledMetrics($startDate: ISO8601Date, $endDate: ISO8601Date, $after: BigInt) { modeledMetrics(startDate: $startDate, endDate: $endDate, after: $after) { data { reportedDate sourceChannelName sourceCampaignId target metricName metricValue } pageInfo { endCursor hasNextPage } } }`;
async function pullModeledMetrics(startDate, endDate) { const rows = []; let after = null; do { const response = await fetch('https://api.prescient-ai.io/graphql', { method: 'POST', headers: { Authorization: `apikey ${process.env.PRESCIENT_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ query: QUERY, variables: { startDate, endDate, after } }), }); const { data, errors } = await response.json(); if (errors) throw new Error(errors[0].message);
const page = data.modeledMetrics; rows.push(...page.data); after = page.pageInfo.hasNextPage ? page.pageInfo.endCursor : null; } while (after !== null); return rows;}
const rows = await pullModeledMetrics('2026-01-01', '2026-01-31');import osimport requests
QUERY = """query ModeledMetrics($startDate: ISO8601Date, $endDate: ISO8601Date, $after: BigInt) { modeledMetrics(startDate: $startDate, endDate: $endDate, after: $after) { data { reportedDate sourceChannelName sourceCampaignId target metricName metricValue } pageInfo { endCursor hasNextPage } }}"""
def pull_modeled_metrics(start_date, end_date): rows = [] after = None while True: response = requests.post( "https://api.prescient-ai.io/graphql", headers={"Authorization": f"apikey {os.environ['PRESCIENT_API_KEY']}"}, json={ "query": QUERY, "variables": {"startDate": start_date, "endDate": end_date, "after": after}, }, ) body = response.json() if body.get("errors"): raise RuntimeError(body["errors"][0]["message"])
page = body["data"]["modeledMetrics"] rows.extend(page["data"]) if not page["pageInfo"]["hasNextPage"]: return rows after = page["pageInfo"]["endCursor"]
rows = pull_modeled_metrics("2026-01-01", "2026-01-31")How do you narrow the result?
Section titled “How do you narrow the result?”Each filter is an argument of the query. You can use more than one filter in a request.
- To get the rows of some campaigns, set
campaignIdsto a list of campaign IDs. The IDs are the IDs of your campaigns in their source channels. - To get one sales channel, set
salesChannelto[ECOMMERCE]or[RETAIL]. If you leave it out, the query returns both. - To get only the rows that changed, set
processDate. See Sync only the rows that changed.
How do you read a row?
Section titled “How do you read a row?”targetis the name of the model that produced the row. Themodelsquery gives the unit of each model.metricNametells you whatmetricValuemeasures. SeeMetricName.targetChannelNamecan benull. The schema says that the field applies only to second order metrics.sourceCampaignNameandsourceCampaignTacticcan benull. The tactic is the label that the campaign has in the Data Center. See Label campaigns with tactics.