Before

After

When creating your Facebook advertising campaigns, you might have experienced the headache of finding the right interests. The suggestions are limited and when using the Audience Insights, most of the times, the suggested interests don't even exist inside the ad platform.

So, I thought it'd be handier to use the Facebook Marketing API in order to find interests and suggestions for a given keyword. That's exactly the aim of this tutorial. Give you a quick way to find those golden niche interests.

For all the details on ad interests and the associated values, I recommend skimming through the API documentation here.

To get started, make a copy of the above Google Sheet and open your script editor by going to "Tools" -> "Script editor".

Step 1 - Create a Facebook access token

To get an access token, you first have the create a Facebook app. You can do this right here.

From there on head over to the Graph API Explorer to generate an access token. 

Step 2 - Increase the lifespan of the access token

Standard access tokens are only valid for around 30 minutes, which isn't too handy if you want to use this script many times.

Luckily you can quite easily extend that duration via the access token debugger.  Open the debugger, paste in your access token, and extend its expiry date. It should now last at least a few months, but it might still be good to set up a reminder somewhere.

Step 3 - Import search query from sheet

The sheet works by taking the access tokens and search query parameters to then make two fetches. One for the ad interests and one for the ad suggestions. But first, we need to import the query parameters.
var accessToken = sheet.getRange(2,2).getValue(); 
var query = sheet.getRange(4,2).getValue(); 
var locale = sheet.getRange(6,2).getValue(); 
var limit = sheet.getRange(8,2).getValue(); 

Step 4 - Fetch audience interests

With the parameters, we can make a request to the /adinterest API.

var apiUrl = 'https://graph.facebook.com/v7.0/search?type=adinterest&q=['+ query +']&locale='+ locale +'S&limit='+ limit +'&access_token=' + accessToken; 
var responseApi = UrlFetchApp.fetch(apiUrl);
var result = JSON.parse(responseApi.getContentText());

Step 5 - Fetch audience suggestions

Similarly, we'll do this for the /adinterestsuggestion

// make fetch to Facebook Marketing API for suggestions
 var apiUrl = 'https://graph.facebook.com/v7.0/search?type=adinterestsuggestion&interest_list=[%22'+ query +'%22]&locale='+ locale +'S&limit='+ limit +'&access_token=' + accessToken; 
var responseApiSuggestion = UrlFetchApp.fetch(apiUrl);
var resultSuggestion = JSON.parse(responseApiSuggestion.getContentText());

Step 6 - Insert values into sheet

Once we've made the two requests, we've stored all the values inside two JSON objects. We now need to push the values from these objects into sorted arrays after which we can insert these arrays inside the sheet.

 // create empty Arrays to fill with the JSON response 
 var name = [];
 var audience_size = [];
 var path = [];
 var topic = [];
 var suggestion = [];
 var suggestionAudience_Size = [];
  
 // find length of result array to set as the end of the loop 
 var arrayLength = result.data.length;
 var arrayLengthSuggestion = resultSuggestion.data.length;
  
 // push results into the arrays for Interests
 for (var i=0; i<arrayLength; i++){
   name.push([result.data[i].name]); 
   audience_size.push([result.data[i].audience_size]); 
   topic.push([result.data[i].topic]); 
   path.push([result.data[i].path.join(" -> ")]); 
 };
  
 // push results into the arrays for Suggestions
 for (var i=0; i<arrayLengthSuggestion; i++){
   suggestion.push([resultSuggestion.data[i].name]); 
   suggestionAudience_Size.push([resultSuggestion.data[i].audience_size]); 
 };
  
 // remove prior data from the sheet in range A13:D & F:G 
 var sheetRange = sheet.getRange("A13:D").clear();
 var sheetRangeSuggestion = sheet.getRange("F13:G").clear();
 sheet.getRange(13, 1, arrayLength, 1).setValues(name);
 sheet.getRange(13, 2, arrayLength, 1).setValues(audience_size);
 sheet.getRange(13, 3, arrayLength, 1).setValues(topic); 
 sheet.getRange(13, 4, arrayLength, 1).setValues(path);
 sheet.getRange(13, 6, arrayLengthSuggestion, 1).setValues(suggestion); 
 sheet.getRange(13, 7, arrayLengthSuggestion, 1).setValues(suggestionAudience_Size);

And that's it! You should now have a very quick way to explore new audience interests and suggestions.


Let me know if this was useful & let's connect on     or