Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions sources/facebook_ads/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
)
from .settings import (
FACEBOOK_INSIGHTS_RETENTION_PERIOD,
ALL_ACTION_BREAKDOWNS,
ALL_ACTION_ATTRIBUTION_WINDOWS,
DEFAULT_INSIGHT_FIELDS,
INSIGHT_FIELDS_TYPES,
Expand Down Expand Up @@ -126,8 +125,8 @@ def facebook_insights_source(
fields: Sequence[str] = DEFAULT_INSIGHT_FIELDS,
attribution_window_days_lag: int = 7,
time_increment_days: int = 1,
breakdowns: TInsightsBreakdownOptions = "ads_insights",
action_breakdowns: Sequence[str] = ALL_ACTION_BREAKDOWNS,
breakdowns: TInsightsBreakdownOptions = None,
action_breakdowns: Sequence[str] = None,
level: TInsightsLevels = "ad",
action_attribution_windows: Sequence[str] = ALL_ACTION_ATTRIBUTION_WINDOWS,
batch_size: int = 50,
Expand All @@ -149,8 +148,8 @@ def facebook_insights_source(
fields (Sequence[str], optional): A list of fields to include in each reports. Note that `breakdowns` option adds fields automatically. Defaults to DEFAULT_INSIGHT_FIELDS.
attribution_window_days_lag (int, optional): Attribution window in days. The reports in attribution window are refreshed on each run.. Defaults to 7.
time_increment_days (int, optional): The report aggregation window in days. use 7 for weekly aggregation. Defaults to 1.
breakdowns (TInsightsBreakdownOptions, optional): A presents with common aggregations. See settings.py for details. Defaults to "ads_insights_age_and_gender".
action_breakdowns (Sequence[str], optional): Action aggregation types. See settings.py for details. Defaults to ALL_ACTION_BREAKDOWNS.
breakdowns (TInsightsBreakdownOptions, optional): A presents with common aggregations. See settings.py for details. Defaults to None (no breakdowns).
action_breakdowns (Sequence[str], optional): Action aggregation types. See settings.py for details. Defaults to None (no action breakdowns).
level (TInsightsLevels, optional): The granularity level. Defaults to "ad".
action_attribution_windows (Sequence[str], optional): Attribution windows for actions. Defaults to ALL_ACTION_ATTRIBUTION_WINDOWS.
batch_size (int, optional): Page size when reading data from particular report. Defaults to 50.
Expand Down Expand Up @@ -186,16 +185,8 @@ def facebook_insights(
while start_date <= end_date:
query = {
"level": level,
"action_breakdowns": list(action_breakdowns),
"breakdowns": list(
INSIGHTS_BREAKDOWNS_OPTIONS[breakdowns]["breakdowns"]
),
"limit": batch_size,
"fields": list(
set(fields)
.union(INSIGHTS_BREAKDOWNS_OPTIONS[breakdowns]["fields"])
.difference(INVALID_INSIGHTS_FIELDS)
),
"fields": list(set(fields).difference(INVALID_INSIGHTS_FIELDS)),
"time_increment": time_increment_days,
"action_attribution_windows": list(action_attribution_windows),
"time_ranges": [
Expand All @@ -207,6 +198,22 @@ def facebook_insights(
}
],
}

# Only add action_breakdowns if explicitly provided
if action_breakdowns is not None:
query["action_breakdowns"] = list(action_breakdowns)

# Only add breakdowns if explicitly provided
if breakdowns is not None:
query["breakdowns"] = list(
INSIGHTS_BREAKDOWNS_OPTIONS[breakdowns]["breakdowns"]
)
# Add breakdown fields to the fields list
query["fields"] = list(
set(query["fields"])
.union(INSIGHTS_BREAKDOWNS_OPTIONS[breakdowns]["fields"])
.difference(INVALID_INSIGHTS_FIELDS)
)
job = execute_job(account.get_insights(params=query, is_async=True))
yield list(map(process_report_item, job.get_result()))
start_date = start_date.add(days=time_increment_days)
Expand Down
1 change: 0 additions & 1 deletion sources/facebook_ads/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@
"28d_view",
)

ALL_ACTION_BREAKDOWNS = ("action_type", "action_target_id", "action_destination")

INSIGHTS_BREAKDOWNS_OPTIONS: Dict[TInsightsBreakdownOptions, Any] = {
"ads_insights": {"breakdowns": (), "fields": ()},
Expand Down
20 changes: 20 additions & 0 deletions sources/facebook_ads_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,30 @@ def load_insights() -> None:
print(info)


def load_insights_with_breakdowns() -> None:
"""Shows how to load insights with custom breakdowns and action breakdowns"""
pipeline = dlt.pipeline(
pipeline_name="facebook_insights_breakdowns",
destination="duckdb",
dataset_name="facebook_insights_data",
dev_mode=True,
)
# Load insights with age and gender breakdowns
i_with_breakdowns = facebook_insights_source(
initial_load_past_days=7,
breakdowns="ads_insights_age_and_gender",
# Uncomment to add action breakdowns:
# action_breakdowns=["action_type", "action_target_id"]
)
info = pipeline.run(i_with_breakdowns)
print(info)


if __name__ == "__main__":
# load_all_ads_objects()
merge_ads_objects()
# load_ads_with_custom_fields()
# load_only_disapproved_ads()
# load_and_enrich_objects()
# load_insights()
# load_insights_with_breakdowns()
Loading