How to Automate Facebook Canvas Creation with R Studio


Facebook’s shiny new toy

If you haven’t heard, Facebook’s new Canvas ads for mobile are blowing up. They’re full screen, immersive, and generally a lot more fun than the typical Facebook video or carousel ad. And they address the number one problem for mobile — page load time — by having all the underlying image, text and video content hosted on Facebook’s platform. They load in the blink of an eye rather than the 4-10 seconds for the average non-Facebook hosted mobile web page.

Sample Canvas Ad: Carnival Cruise Line ( click for video )

Sample Canvas Ad: Carnival Cruise Line (click for video)

But creating more than one of them is a big pain. There is no Facebook Power Editor for Canvas ad creation, but Facebook does allow for automated Canvas creation via API. This means it is possible to create customized Canvases at scale, but it isn’t easy.

We decided to change that. And we wanted to do it using open source software we already used for marketing analytics. So we created an application in R Studio — the integrated development environment for the programming language R.

R isn’t just a tool for statistical analysis any more. It’s also a powerful platform for creating programmatic advertising campaigns. The development of packages such as ‘httr’, originally for web-scraping, has a nice secondary use of being able to interact with APIs such as Facebook’s.  Since httr translates R into cURL you can create, edit, and analyze advertising campaigns on a single platform.


Why we took on the Canvas challenge

A central feature of our Serendipity advertising system is the ability to target a client’s ads down to the ZIP/post code level. Our objective is to get the client’s brand names and products into consumers’ minds close to the physical point of purchase. And we only want to promote specific products at locations that we know to carry them.

Given load time is the number one reason for mobile site abandonment, we wanted to use Facebook Canvases instead of standard microsite landing pages.

While fast-loading and highly engaging, Canvases are not flexible — they cannot be edited after they are published. This means they cannot contain dynamic elements that change based on URL parameters like a typical landing page. Therefore, we’d need to create one Canvas for every ad permutation.  And we’d need the infrastructure in place to quickly update all of the Canvases automatically for content refreshes and design testing.

So we began the journey deep into Facebook’s marketing API.


There be monsters in this documentation

The documentation for Facebook’s API is extensive, but as one would expect, far from perfect. A big problem is the sometimes haphazard way documentation pages are presented in Google search results — older, deprecated documentation can appear ahead of newer more relevant pages. Unless someone, like us, has written an article for accomplishing a specific goal, then it’s a combination of critical analysis, and trial and error to get to the correct answer.

Further confusing matters, Facebook Apps for use as page tabs or games also use the name ‘canvas’, but are unrelated. Mobile Canvas ads are sometimes referred to as ‘Collection Ads’ and ‘Full Screen Experience’. Again this doesn’t make the task of locating the correct answers any easier.


A quick guide to creating Canvases in R

While some additional reading and research will still be necessary, below is a quick guide to getting started with programmatically creating canvases.


The Key Steps to Creating a Facebook Canvas Template in R

  1. Design and publish a Canvas in the Facebook Canvas Builder to serve as a template and grab the canvas ID from the canvas URL.
  2. Create a Facebook App in Business Manager to generate the access tokens and privileges to associate the app with the brand page where the canvas assets will live.
    • It’s important to note that your Canvases are associated with brand pages rather than ad accounts, hence the need to pay attention to permissions and access tokens.
    • Your app does not need to be approved for public use to gain the specific privileges needed.
  3. Grab all the elements and formatting for the canvas through the API by using Graph API Explorer to generate the cURL code to be translated by httr.
  4. Flatten out each element and formatting option in the returned JSON element into variables in a tidy R dataframe.
  5. Place your image and video assets onto a cloud account such as S3, grab the deep links and add these as variables to the frame.


You now have a template data frame. Each row represents an individual Canvas and each column represents a content or formatting parameter for the Canvas. You can then populate the dataframe in R with whichever data set you wish. Here comes the trickier part — grabbing all the element IDs.

Canvas element IDs in R Studio

Canvas element IDs in R Studio

This post shows that from the base elements of photos and text there is a specific order in which you must post assets to get element IDs. Once you have the final element IDs, these are POST-ed in their vertical order and a Canvas ID is returned. You can easily construct the Canvas URL by adding the ID to:

This is now the target / click-through URL for your ads whether you create them through the API or Power Editor/Ads Manager. The neat thing is when you add a Canvas URL into a create / edit ad screen in Facebook Ad Manager or Power Editor, the ad format automatically changes to one of the Canvas options.

And that’s it. Each Canvas takes about 30 to 45 seconds to create.


"A man who has been through bitter experiences and travelled far enjoys even his sufferings after a time." — Homer, The Odyssey


What we’ve learned from this journey

There doesn’t seem to be any upper limit of the number of Canvases that can be created nor the number of image assets that can be uploaded to a page for use within Canvases. That’s both a good and a bad thing — but, we’d prefer to know so we could plan against it. Since brand pages are free, hitting an upper limit would just mean using a new brand page to store assets and create Canvases.

Once you’ve created scores of Canvases, using the web-based Facebook tools for managing Canvases and creating Canvas-based custom audiences becomes difficult. It’s easy to get the feeling the management tools were an afterthought to the main Canvas product and certainly don’t accommodate managing an abundance of Canvases. Once you start to use the API to create Canvases and ads at scale, plan to use the API to manage at scale.

The performance measurement of Canvases is tied to the ads that link to them. Again, the easiest way to do this is through the Facebook API, specifically the Insights API. There are three relevant metrics:

  • Clicks-through

  • Time spent within a Canvas

  • %age of Canvas viewed

A query to the Insights API at the ‘ad’ level will return a lot of data. This is where R is your friend — filtering and summarizing tens of thousands of rows of data takes less than a second.

In conclusion, this final point emphasizes why R, once the purview of academia, is becoming a powerful multi-purpose programming language. Within a single script one can create Facebook Canvases, ads, targeting criteria, and analyze the campaign performance.