I was reading an article about creating a "Growth Score" and it included the number of social media followers/fans/likes.  I wasn't ready to implement the growth score as a whole, but it did get me to thinking about importing facebook page like counts into Rock as a metric. After wrestling with the API and a little help from Steve Lipinski, I got it working.

Disclaimer: I already created a facebook app for authentication. I used that app to create the token even though I do not use it for authentication anymore. Creating an application is beyond the scope of a recipe but is in the Documentation.

First Create the Token: 

Visit the Facebook Graph API Explorer.  I had better luck with the older version rather than the new beta version.  Follow these steps for a token that will only work with this page. 

    1. Select the application you are using.

    2. Select the page you want to create the token for,

    3. Click Submit,

    4. Copy the token. 

access Token.png

This is a temporary token, so we need to extend the life.  After you copy the generated token, visit the Token Debugger and paste your token into the text field and click debug.  

On this next screen -

    1. Paste your access token and click Debug. 

    2. Click Debug

    3. Click Extend Access Token

    4. Copy and save Page Id

    5. Copy and save New Token(This token will not expire).

extended Token.png

Then Create Global 2 Global Attributes:

    1. FacebookPageID - set value equal to page Id

    2. FacebookPageAccessToken - Set value equal to extended page token.

Create Your Metric: 

I created a metric category called social media and a metric called Facebook Page Likes.  Set your sorce type to Lava and use this code. I set my schedule to once a week on thursdays at 1:00pm.  You can set your to whatever you

    {% assign CurrentPerson = 1 | PersonById %}
{% assign token = 'Global' | Attribute:'FacebookPageAccessToken','RawValue' %}
{% assign page = 'Global' | Attribute:'FacebookPageID' %}
{% webrequest url:'https://graph.facebook.com/{{page}}?access_token={{token}}&fields=fan_count' %}
    {{ results.fan_count }}
{% endwebrequest %}