This repository contains two Google Apps Script files designed to automate the process of importing Facebook Ads insights data into Google Sheets and archiving it for historical analysis.
This script fetches Facebook Ads insights data (e.g., impressions, clicks, spend) and writes it into a specific Google Sheet.
- API Request: Fetches data from the Facebook Graph API using a URL with parameters such as:
account ID
access token
- Desired fields like campaign, ad set, and ad performance metrics.
- Pagination: Handles paginated API responses to ensure all data is fetched.
- Data Transformation: Converts the JSON response into a tabular format suitable for Google Sheets.
- Google Sheets Integration: Writes the fetched data into a Google Sheet tab named
"test-fb"
, starting from the second row.
- Replace
{ACCOUNT_ID}
and{ACCESS_TOKEN}
in the script with your actual credentials. - Update the sheet name (
"test-fb"
) to match the tab name in your Google Sheet. - Ensure the Google Sheet is prepared with appropriate column headers.
This script transfers daily data from the "test-fb"
sheet to another sheet ("data - by day"
) for maintaining a historical log.
- Source Data Extraction:
- Reads data from the
"test-fb"
sheet, starting from row 2 (to exclude headers). - Filters out any blank rows.
- Reads data from the
- Destination Preparation:
- Identifies the next available row in the
"data - by day"
sheet. - Clears the destination range before inserting new data.
- Identifies the next available row in the
- Data Transfer:
- Appends filtered data to the
"data - by day"
sheet.
- Appends filtered data to the
- Ensure the
"data - by day"
tab exists in your Google Sheet. - The script assumes the source sheet (
"test-fb"
) has data fetched by theimportDataFromJSON.gs
script.
importDataFromJSON.gs
: Fetches and writes fresh Facebook Ads data for "yesterday" into the"test-fb"
sheet.copyDataOnceADay.gs
: Moves this data to the"data - by day"
sheet for archival purposes.
- On Day 1, run
importDataFromJSON()
to fetch and write data into the"test-fb"
sheet. - On Day 2, run
copyDataOnceADay()
to archive this data into the"data - by day"
sheet.
- A Google Sheet with two tabs:
"test-fb"
: For temporarily storing imported data."data - by day"
: For archiving daily data.
- Facebook Ads account credentials to generate:
- Account ID
- Access Token
- Open your Google Sheets file.
- Go to Extensions > Apps Script.
- Copy-paste the content of both
importDataFromJSON.gs
andcopyDataOnceADay.gs
into separate.gs
files. - Save and deploy the project.
importDataFromJSON()
: Schedule this function to run daily (e.g., midnight) to fetch the previous day's data.copyDataOnceADay()
: Schedule this function to run shortly after the import to archive the data.
-
API Fetch Example:
RunimportDataFromJSON()
to populate your"test-fb"
sheet with yesterday's Facebook Ads data. -
Data Transfer Example:
RuncopyDataOnceADay()
to move the data to the"data - by day"
sheet, ensuring a daily log is maintained.
- Error Handling:
- Add checks for successful API calls (e.g., handling rate limits, invalid tokens).
- Ensure both source and destination sheets exist before running.
- Dynamic Date Handling:
- Allow users to specify custom date ranges or dynamically calculate them.
- Logging:
- Log successful operations, errors, and the number of rows transferred.
Feel free to contribute or raise an issue if you encounter any bugs or have feature requests!