Skip to content

Commit 56085b1

Browse files
committed
Add adAnalytics example demonstrating the finder method (#32)
Addresses a common source of confusion around querying the adAnalytics endpoint. Shows three usage patterns: account-level analytics by campaign, daily analytics for specific campaigns, and creative-level breakdown. Demonstrates passing dateRange dicts, URN lists, and the fields parameter as native Python types. Closes #32
1 parent 6331e52 commit 56085b1

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

examples/get_ad_analytics.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
"""
2+
Example calls to fetch ad analytics data using the adAnalytics finder.
3+
4+
The 3-legged member access token should include the 'r_ads_reporting' scope, which is part of the
5+
Advertising APIs product.
6+
7+
For full documentation on the adAnalytics API, see:
8+
https://learn.microsoft.com/en-us/linkedin/marketing/integrations/ads-reporting/ads-reporting
9+
"""
10+
11+
import os, sys
12+
13+
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
14+
from dotenv import load_dotenv, find_dotenv
15+
16+
load_dotenv(find_dotenv())
17+
18+
from linkedin_api.clients.restli.client import RestliClient
19+
import json
20+
21+
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
22+
if ACCESS_TOKEN is None:
23+
raise Exception(
24+
'A valid access token must be defined in the /examples/.env file under the variable name "ACCESS_TOKEN"'
25+
)
26+
27+
AD_ANALYTICS_RESOURCE = "/adAnalytics"
28+
API_VERSION = "202404"
29+
30+
restli_client = RestliClient()
31+
restli_client.session.hooks["response"].append(lambda r: r.raise_for_status())
32+
33+
34+
"""
35+
Get analytics for a specific ad account, broken down by campaign.
36+
37+
Required query parameters:
38+
- dateRange: Start and end dates for the report
39+
- timeGranularity: DAILY, MONTHLY, or ALL
40+
- pivot: The entity type to group results by (e.g. CAMPAIGN, CREATIVE, ACCOUNT)
41+
- One of: accounts, campaigns, creatives, or campaignGroups (as a list of URNs)
42+
43+
Note: Query parameter values should be passed as native Python types (lists, dicts).
44+
The client handles Rest.li protocol encoding automatically.
45+
"""
46+
response = restli_client.finder(
47+
resource_path=AD_ANALYTICS_RESOURCE,
48+
finder_name="statistics",
49+
query_params={
50+
"pivot": "CAMPAIGN",
51+
"dateRange": {
52+
"start": {"day": 1, "month": 1, "year": 2024},
53+
"end": {"day": 31, "month": 12, "year": 2024},
54+
},
55+
"timeGranularity": "MONTHLY",
56+
"accounts": ["urn:li:sponsoredAccount:123456789"],
57+
"fields": "impressions,clicks,costInLocalCurrency,dateRange",
58+
},
59+
access_token=ACCESS_TOKEN,
60+
version_string=API_VERSION,
61+
)
62+
print("Ad analytics by campaign:")
63+
print(json.dumps(response.elements, indent=2))
64+
print(f"Total results: {response.paging.total}\n")
65+
66+
67+
"""
68+
Get daily analytics for specific campaigns with additional metrics.
69+
"""
70+
response = restli_client.finder(
71+
resource_path=AD_ANALYTICS_RESOURCE,
72+
finder_name="statistics",
73+
query_params={
74+
"pivot": "CAMPAIGN",
75+
"dateRange": {
76+
"start": {"day": 1, "month": 1, "year": 2024},
77+
"end": {"day": 31, "month": 1, "year": 2024},
78+
},
79+
"timeGranularity": "DAILY",
80+
"campaigns": [
81+
"urn:li:sponsoredCampaign:123456789",
82+
"urn:li:sponsoredCampaign:987654321",
83+
],
84+
"fields": "impressions,clicks,costInLocalCurrency,externalWebsiteConversions,dateRange,pivotValues",
85+
},
86+
access_token=ACCESS_TOKEN,
87+
version_string=API_VERSION,
88+
)
89+
print("Daily analytics for specific campaigns:")
90+
print(json.dumps(response.elements, indent=2))
91+
print(f"Total results: {response.paging.total}\n")
92+
93+
94+
"""
95+
Get analytics broken down by creative.
96+
"""
97+
response = restli_client.finder(
98+
resource_path=AD_ANALYTICS_RESOURCE,
99+
finder_name="statistics",
100+
query_params={
101+
"pivot": "CREATIVE",
102+
"dateRange": {
103+
"start": {"day": 1, "month": 1, "year": 2024},
104+
"end": {"day": 31, "month": 1, "year": 2024},
105+
},
106+
"timeGranularity": "ALL",
107+
"campaigns": ["urn:li:sponsoredCampaign:123456789"],
108+
"fields": "impressions,clicks,costInLocalCurrency,pivotValues",
109+
},
110+
access_token=ACCESS_TOKEN,
111+
version_string=API_VERSION,
112+
)
113+
print("Analytics by creative:")
114+
print(json.dumps(response.elements, indent=2))
115+
print(f"Total results: {response.paging.total}\n")

0 commit comments

Comments
 (0)