Skip to content

Commit 94914f9

Browse files
Jussan Da Silva Bahia Nascimentometa-codesync[bot]
authored andcommitted
Add Lift Study example (#814)
Summary: This example shows how to retrieve a Conversion Lift Study (CLS) and its results using the Facebook Business SDK. Pull Request resolved: #814 Reviewed By: satwikareddy3, JussanN Differential Revision: D109317515 Pulled By: stcheng fbshipit-source-id: 41c6107cfde883361660d2c25150f93e2bf8ecb5
1 parent 54bc252 commit 94914f9

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

examples/getliftstudy.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
4+
# This source code is licensed under the license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# ============================================================================
8+
# Get Lift Study Results Example
9+
# ============================================================================
10+
#
11+
# This example shows how to retrieve a Conversion Lift Study (CLS) and its
12+
# results using the Facebook Business SDK.
13+
#
14+
# Prerequisites:
15+
# 1. Create a Meta App and retrieve your App ID and App Secret.
16+
# - Go to https://developers.facebook.com/apps/ and select your app.
17+
# - App ID is at the top of the dashboard.
18+
# - App Secret is under Settings > Basic (click "Show" to reveal).
19+
#
20+
# 2. Generate a System User access token with permissions:
21+
# ads_management, ads_read, business_management.
22+
# - Go to Business Manager > Business Settings > Users > System Users.
23+
# - Assign your Meta App (Add Assets > Applications).
24+
# - Click "Generate Token" and select the permissions above.
25+
#
26+
# 3. Install dependencies:
27+
# pip install facebook-business pandas
28+
# ============================================================================
29+
30+
import json
31+
import pandas as pd
32+
from facebook_business.api import FacebookAdsApi
33+
from facebook_business.adobjects.adstudy import AdStudy
34+
35+
# --- Configuration -----------------------------------------------------------
36+
APP_ID = '<YOUR_APP_ID>'
37+
APP_SECRET = '<YOUR_APP_SECRET>'
38+
ACCESS_TOKEN = '<YOUR_ACCESS_TOKEN>'
39+
STUDY_ID = '<YOUR_STUDY_ID>'
40+
41+
# --- Initialize the SDK ------------------------------------------------------
42+
FacebookAdsApi.init(APP_ID, APP_SECRET, ACCESS_TOKEN)
43+
44+
# --- Retrieve Study dates ----------------------------------------------------
45+
study = AdStudy(STUDY_ID)
46+
study_data = study.api_get(fields=[
47+
AdStudy.Field.name,
48+
AdStudy.Field.start_time,
49+
AdStudy.Field.end_time,
50+
])
51+
print("Study:", study_data.get('name'))
52+
print("Start:", study_data.get('start_time'))
53+
print("End: ", study_data.get('end_time'))
54+
55+
# --- Retrieve Study cells (treatment / control) ------------------------------
56+
cells = list(study.get_cells())
57+
print("\nCells:", cells)
58+
59+
# --- Retrieve objectives and parse results ------------------------------------
60+
objectives = study.get_objectives(fields=['id', 'results'])
61+
62+
all_results = []
63+
for objective in objectives:
64+
for result_string in objective.get('results', []):
65+
data = json.loads(result_string)
66+
data['study_start_time'] = study_data.get('start_time')
67+
data['study_end_time'] = study_data.get('end_time')
68+
data['objective_id'] = objective.get('id')
69+
all_results.append(data)
70+
71+
df = pd.DataFrame(all_results)
72+
print("\nResults:")
73+
print(df.head())

0 commit comments

Comments
 (0)