Export Facebook Ads Data to Google Spreadsheets

Mert Yüksel
5 min readAug 29, 2022

--

Introduction

A friend of mine has recently asked me to develop a script that would pull Facebook ads data into Google Sheets. When I delivered the script, I saw that it was really useful for my friend. Then I thought it might help others too. And, as a result, this blog post showed up.

You don’t need any software knowledge to run the script. You just need to provide some necessary parameters for the script to run. Without wasting any more time, let’s get started.

Opening Google App Script Project

To be able to run our script, we need to open a google app script project. Go to script.google.com. Click New Project in the upper left corner. (if you are redirected to google.com/script/start/ click Start Scripting and log in to your Google account)

You can give your project any name you want, I’ll say fbAdsProject. Then, delete the function that comes with the project. Now, we are ready to paste our script here.

Go to this link. Copy the script and paste it into the newly opened google app script project.

As you can see in the script there are some required fields to be filled. Before running our script we must provide the required parameters to it. Let’s start with the first step.

Getting token for Facebook Graph API

Our script makes use of Facebook Graph API to pull the data into a spreadsheet. To be able to use Facebook Graph API we need to get an Access Token.

  1. Go to https://developers.facebook.com/ address (if not logged in, log in to your Facebook account from the upper right corner)
  2. Click My Apps (if My Apps does not exist at the right top corner click get started and create your Facebook developer account within seconds, then the My Apps button will show up)
  3. Click Create an App.
  4. Choose app type as Business, then Next.
  5. Enter an App Display Name. It could be any name you want. I’ll name the app Sheet Connection App. Enter a contact email, and select “Yourself or your own business” in the app purpose part. Then click Create App.
  6. You will be directed to the “Add Products to Your App” page. Here find the Marketing API and click Set Up.
  7. Then click Tools under the Marketing API title on the left sidebar.
  8. In the Get Access Token part, check ads_read and click Get Token.
  9. Then the token will show up at the bottom.

It was the hardest part actually and we finally got the token. Copy it and go back to our google app script project. Paste the token in its place as follows.

Finding your ad account ID

Next, we are gonna need the Facebook Ad Account ID. Go to Ads Manager. Your Ad Account ID number is shown above the search and filter bar, in the account dropdown menu.

Also, as another way, it will be in your browser’s address bar after the “act=” letters. Then, in the same way, let’s paste it into the account_id, in our script.

Copy and paste it in its place in the Google App Script Project.

Lastly, we need the spreadsheet information, then the script will be ready to run. Let’s copy the URL of the spreadsheet in which we want to see our data and paste it into the SPREADSHEET_URL part. Enter your spreadsheet tab name into the SHEET_TAB_NAME part.

Running the script

Finally, our script is ready to run.

Save the project

Choose the getFacebookAdsData function at the top and press Run

Since this is the first run of the Google App Script project, Google will ask for one-time permission.

1. Click Review permissions

2. Choose your Google account

3. Click on Advanced then click on Go to fbAdsProject (unsafe).

It is safe since you are the creator of the project.

4. Press Allow. Then execution will start.

After the execution is complete, you can check your spreadsheet to see if the data has been pulled.

That permission part was only for the first execution of the script. The next time you click on Run, the execution will start immediately.

Options

You may have noticed that our script also has optional parameters. Using these, we can customize the data we will extract. For example, you can extract your data, broken down by age and gender, by doing the following

Or you could specify the fields you want to extract.

Conclusion

You can do a lot of customization like above. Since there are so many customization possibilities, I won’t go into all these details here. You can make use of Facebook Marketing API documentation to learn what customizations are possible and how they are implemented.

However, if you have any questions about customization or any other issue, do not hesitate to contact me (mert.yksl@outlook.com). I would be happy to help.

--

--