Skip to main content

Collect and store Facebook Insights metrics using Django models.

This app provides a flexible abstract model with method fetch(). It gets all required metrics with a single batch request to Graph API and puts them into your child model’s fields.

Requirements:

  • Django 1.7 to 1.10 on Python 2.7

  • Django 1.8 to 1.10 on Python 3.4 and 3.5

  • facebook-sdk 1.0.0+

Installation

Install the app with pip:

$ pip install django-facebook-insights

Add ‘facebook_insights’ to your INSTALLED_APPS setting:

INSTALLED_APPS = [
    ...
    'facebook_insights',
]

Finally, provide a valid access token with the ‘read_insights’ permission using setting FACEBOOK_INSIGHTS_ACCESS_TOKEN.

Usage example

In the simplest case, all you need to do is to write code similar to the one below:

from django.db import models
from facebook_insights.models import Insights


# Inherit from the abstract 'Insights' model
class PostInsights(Insights):
    # List metrics you're intrested in
    METRICS = [
        'post_impressions',
        'post_impressions_unique',
        'post_stories',
        'post_stories_by_action_type',
    ]
    # Define field 'graph_id' to hold the Facebook ids of objects
    graph_id = models.CharField(max_length=100)
    # Define fields to store the metrics
    impressions = models.PositiveIntegerField()
    impressions_unique = models.PositiveIntegerField()
    stories = models.PositiveIntegerField()
    stories_by_action_type = models.CharField(max_length=100)

Now, you can instantiate the model and call fetch() to get the metrics from Facebook’s servers:

>>> post_insights = PostInsights(graph_id=your_post_id)
>>> post_insights.fetch()
>>> post_insights.impressions
1000
>>> post_insights.impressions_unique
200
>>> post_insights.stories
100
>>> post_insights.stories_by_action_type
'{"like": 40, "share": 30, "comment": 30}'

Mapping metrics to fields

To figure out which metrics belong to which fields, the app uses the following simple algorithm:

  • Take the metric name as the base name.

  • Remove the object type prefix ('post_', 'page_' or 'domain_'), if attribute REMOVE_PREFIX is set to True (the default). The prefix is removed, so we can, for instance, access the ‘post_impressions’ metric as post_insights.impressions instead of post_insights.post_impressions.

If you want to use a more complex algorithm, you need to override the get_field_name() method.

Extracting field values

Values associated with page metrics are quite complex. They are available for several periods (e.g. day, week, 28 days) and include data for 3 consecutive days. By contrast, values of most of post metrics are available only for one period (lifetime) and represent the current state of things.

The extraction of metric values is the responsibility of the get_field_value() method. The default implementation works as follows:

  • If a metric has several periods, return the dictionary of mappings between the periods and the last available values for these periods serialized into JSON, for example, ‘{“day”: 10, “week”: 70, “days_28”: 300”}’. The data for previous days are discarded.

  • If a metric is provided only for a single period, then simply return the value (serialize, if it’s not a number).

Feel free to override the method, if you want something else.

Reporting bugs

If you’ve found a bug:

Contributing code

  • Fork the project on GitHub to your account.

  • Clone the repository:

    $ git clone https://github.com/nevimov/django-facebook-insights
  • Setup a virtual environment:

    $ virtualenv venv
    $ source venv/bin/activate
    $ pip install -U pip
    $ pip install django
    $ pip install -r requirements.txt
  • In directory ‘tests’ create a file named ‘secret.py’. In this file, set

    the FACEBOOK_INSIGHTS_ACCESS_TOKEN setting’. Alternatively, define an environment variable with the same name.

  • Run tests to ensure everything is OK:

    $ python runtests.py

    You can use -h or –help to see options available to the script.

  • Create a topic branch and commit your changes there.

  • Push the branch up to GitHub.

  • Create a new pull request.

Release files for django-facebook-insights 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-facebook-insights 0.1.0
File Size Uploaded
django-facebook-insights-0.1.0.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-facebook-insights 0.1.0
File Interpreter ABI Platform
django_facebook_insights-0.1.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 21.4 kB

Release files / django-facebook-insights-0.1.0.tar.gz

Download URL django-facebook-insights-0.1.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
0bd9b6dd037e89cda6fd6c2141c5357903b3f0889a62937316f138577a7b5552
BLAKE2b-256 checksum
How to use checksums
1398a28745e0c364c1405727de6278f582ed153f420795d81219091e39fba685
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_facebook_insights-0.1.0-py2.py3-none-any.whl

Download URL django_facebook_insights-0.1.0-py2.py3-none-any.whl
Size 12.5 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d2b665a05f6ba211c44d7e27e506aea48a160acc13316f73530064761cfc4a2d
BLAKE2b-256 checksum
How to use checksums
7cb95484c539fdb4fe6ecd5bfc53958bb3c0f365da8790005e862f8cc882a40a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page