Getting Ad Insights from Facebook – and avoiding error 17

Wednesday, January 13th, 2016

At untapt measuring the success of our marketing campaigns across social media is important – to us and our clients. If we are going to put dollars into a campaign to find the best tech talent around the world, we need to know which social media platforms are performing best.

I’ve struggled with the Facebook API so I thought I would do a quick recap of the solution I implemented in Python. In doing so, I had to make sure that the number of API calls was limited so avoid Error 17 – user request limit reached.

The most important discovery in the code below was that insights can be retrieved for the account, but to see them aggregated at the ad level you simply need to pass the ‘level’ parameter.

Shout out to http://jakzaprogramowac.pl/pytanie/12122,fb-ads-api-17-user-request-limit-reached for pointing me in the right direction and also to this Stackoverflow response which was essential in helping me converting the access token I retrieved from the Facebook Graph Explorer to a long lived access token.

Setting up Heroku, Flask and Python on a Windows 7 PC

Friday, December 20th, 2013

Following the steps on the Heroku site for getting started with python guide I ran into a few issue-ettes.  Below are some scrappy notes that could be used to accompany the guide if you run into the same issues that I did.

Init()

Before using the quickstart guide on the Heroku site itself, you’ll need to have ssh-keygen installed.  The Open-SSH package for Windows can be downloaded from here: http://sshwindows.sourceforge.net/download.  On installation you don’t need the server component and the PATH variable should be updated automatically to include the location of \bin\ssh-keygen.exe.  WARNING: My PATH was destroyed twice after installing SSH.  I would back it up first!

Once the Heroku toolbelt is installed and you login for the first time, when prompted to generate an SSH public key, the process will find and run ssh-keygen.exe.

Can’t find Foreman

I followed the steps here suggested by Mark Rajcok: http://stackoverflow.com/questions/19078939/foreman-installed-by-heroku-toolbelt-on-windows-cant-be-found successfully to get Foreman working.  Note also below the comment on running without gunicorn.

The main points for me were:

  • install Heroku to c:\heroku (nowhere else)
  • add C:\Heroku\ruby-1.9.2\bin to the PATH
  • I did not need to un/install Foreman or downgrade it from version 0.63

Sample Python code gives a 404 error

The hello world Python code here lacks code to run the app.  This is not the complete solution with respect to a Heroku deployment – see further below.

Foreman found, but python code still does not run.

Don’t use gunicorn, so the contents of the Procfile should therefore be:

Push rejected, no Cedar-supported app, detected.

Did you create the requirements.txt file?

 Heroku deployed but app crashes with: “Web process failed to bind to $PORT”

Heroku assigns the PORT, so the Flask default PORT of 5000 cannot be used.  The python code should therefore be changed thus:

 No web processes running

I did not realise that I need to start up the web service manually after each deployment.  Running heroku ps:scale web=1 resolved the issue.  You can then also run

to verify the web process is up.

Resynching a repository or setting up a new machine

re-synch with:

For a new machine generate an ssh key and add to Heroku.  See https://devcenter.heroku.com/articles/keys. When I did this initially at the office I had lots of problems with permissions – i.e:

The problem was that the key was being generated on my HOMEDRIVE (in my work this is F:\.ssh) but Heroku was looking in C:\Users\MYNAME\.ssh.  Copying the id_rsa files from F:\.ssh to C:\Users\MYNAME\.ssh fixed the problem.