

# Connecting to Facebook Page Insights
Connecting to Facebook Page Insights

Facebook Pages allow companies and other interest groups to create pages for the Facebook.com social network. Companies use these pages to share open hours, make announcements, and engage with customers online. If you are a Facebook Page Insights user, you can connect AWS Glue to your Facebook Page Insights account. You can use Facebook Page Insights as a data source in your ETL jobs. Run these jobs to transfer data from Facebook Page Insights to AWS services or other supported applications.

**Topics**
+ [

# AWS Glue support for Facebook Page Insights
](facebook-page-insights-support.md)
+ [

# Policies containing the API operations for creating and using connections
](facebook-page-insights-configuring-iam-permissions.md)
+ [

# Configuring Facebook Page Insights
](facebook-page-insights-configuring.md)
+ [

# Configuring Facebook Page Insights connections
](facebook-page-insights-configuring-connections.md)
+ [

# Reading from Facebook Page Insights entities
](facebook-page-insights-reading-from-entities.md)
+ [

# Facebook Page Insights connection options
](facebook-page-insights-connection-options.md)
+ [

# Limitations and notes for Facebook Page Insights connector
](facebook-page-insights-connector-limitations.md)

# AWS Glue support for Facebook Page Insights


AWS Glue supports Facebook Page Insights as follows:

**Supported as a source?**  
Yes. You can use AWS Glue ETL jobs to query data from Facebook Page Insights.

**Supported as a target?**  
No.

**Supported Facebook Page Insights API versions**  
The following Facebook Page Insights API versions are supported:
+ v17
+ v18
+ v19
+ v20
+ v21

# Policies containing the API operations for creating and using connections
IAM policies

The following sample policy describes the required AWS IAM permissions for creating and using connections. If you are creating a new role, create a policy that contains the following:

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "glue:ListConnectionTypes",
        "glue:DescribeConnectionType",
        "glue:RefreshOAuth2Tokens",
        "glue:ListEntities",
        "glue:DescribeEntity"
      ],
      "Resource": "*"
    }
  ]
}
```

------

If you don't want to use the above method, alternatively use the following managed IAM policies:
+ [AWSGlueServiceRole](https://console.aws.amazon.com/iam/home#policies/arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole) – Grants access to resources that various AWS Glue processes require to run on your behalf. These resources include AWS Glue, Amazon S3, IAM, CloudWatch Logs, and Amazon EC2. If you follow the naming convention for resources specified in this policy, AWS Glue processes have the required permissions. This policy is typically attached to roles specified when defining crawlers, jobs, and development endpoints.
+ [AWSGlueConsoleFullAccess](https://console.aws.amazon.com/iam/home#policies/arn:aws:iam::aws:policy/AWSGlueConsoleFullAccess) – Grants full access to AWS Glue resources when an identity that the policy is attached to uses the AWS Management Console. If you follow the naming convention for resources specified in this policy, users have full console capabilities. This policy is typically attached to users of the AWS Glue console.

# Configuring Facebook Page Insights


Before you can use AWS Glue to transfer data from Facebook Page Insights, you must meet these requirements:

## Minimum requirements


The following are minimum requirements:
+ Facebook Standard accounts are accessed directly through Facebook.
+ User authentication is needed to generate the access token.
+ The Facebook Page Insights connector implements the User Access Token OAuth flow.
+ The connector uses OAuth2.0 to authenticate our API requests to Facebook Page Insights. This falls under Multi-Factor Authentication (MFA) architecture, which is a superset of 2FA. It is web-based authentication.
+ User needs to grant permissions to access the endpoints. For accessing the user's data, endpoint authorization is handled through permissions and features.

# Configuring Facebook Page Insights connections


To configure a Facebook Page Insights connection:

1. In AWS Glue Glue Studio, create a connection under **Data Connections** by following the steps below:

   1. When selecting a **Connection type**, select Facebook Page Insights.

   1. Select the AWS IAM role which AWS Glue can assume and has permissions for following actions:

------
#### [ JSON ]

****  

      ```
      {
        "Version":"2012-10-17",		 	 	 
        "Statement": [
          {
            "Effect": "Allow",
            "Action": [
              "secretsmanager:DescribeSecret",
              "secretsmanager:GetSecretValue",
              "secretsmanager:PutSecretValue",
              "ec2:CreateNetworkInterface",
              "ec2:DescribeNetworkInterfaces",
              "ec2:DeleteNetworkInterface"
            ],
            "Resource": "*"
          }
        ]
      }
      ```

------

   1. Select the authorization code URL.

   1. Select the `secretName` which you want to use for this connection in AWS Glue to put the tokens.

   1. Select the network options if you want to use your network.

1. Grant the IAM role associated with your AWS Glue job permission to read `secretName`.

# Reading from Facebook Page Insights entities


**Prerequisite**

A Facebook Page Insights object you would like to read from. You will need the object name.

**Supported entities for source**:


| Entity | Can be filtered | Supports limit | Supports Order by | Supports Select \$1 | Supports partitioning | 
| --- | --- | --- | --- | --- | --- | 
| Page Content | Yes | No | Yes | Yes | Yes | 
| Page CTA Clicks | Yes | No | No | Yes | Yes | 
| Page Engagement | Yes | No | No | Yes | Yes | 
| Page Impressions | Yes | No | No | Yes | Yes | 
| Page Posts | Yes | No | No | Yes | Yes | 
| Page Post Engagement | No | No | No | Yes | No | 
| Page Post Reactions | No | No | No | Yes | No | 
| Page Reactions | Yes | No | No | Yes | Yes | 
| Stories | Yes | No | No | Yes | Yes | 
| Page User Demographics | Yes | No | No | Yes | Yes | 
| Page Video Views | Yes | No | No | Yes | Yes | 
| Page Views | Yes | No | No | Yes | Yes | 
| Page Video Posts | Yes | No | No | Yes | Yes | 
| Pages | No | Yes | No | Yes | No | 
| Feeds | Yes | Yes | No | Yes | Yes | 

**Example**:

```
facebookPageInsights_read = glueContext.create_dynamic_frame. from options(
    connection_type="facebookpageinsights",
    connection_options={
        "connectionName": "connectionName",
        "ENTITY_NAME": "entityName",
        "API_VERSION": "v21"
   }
```

**Facebook Page Insights field details**:

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/glue/latest/dg/facebook-page-insights-reading-from-entities.html)

## Partitioning queries


**Filter-based partitioning**:

You can provide the additional Spark options `PARTITION_FIELD`, `LOWER_BOUND`, `UPPER_BOUND`, and `NUM_PARTITIONS` if you want to utilize concurrency in Spark. With these parameters, the original query would be split into `NUM_PARTITIONS` number of sub-queries that can be executed by Spark tasks concurrently.
+ `PARTITION_FIELD`: the name of the field to be used to partition the query.
+ `LOWER_BOUND`: an **inclusive** lower bound value of the chosen partition field.

  For the Datetime field, we accept the Spark timestamp format used in Spark SQL queries.

  Examples of valid value:

  ```
  "2024-09-30T01:01:01.000Z"
  ```
+ `UPPER_BOUND`: an **exclusive** upper bound value of the chosen partition field.
+ `NUM_PARTITIONS`: the number of partitions.

Example:

```
facebookPageInsights_read = glueContext.create_dynamic_frame.from_options(
     connection_type="facebookpageinsights",
     connection_options={
         "connectionName": "connectionName",
         "ENTITY_NAME": "entityName",
         "API_VERSION": "v21",
         "PARTITION_FIELD": "created_Time"
         "LOWER_BOUND": "2024-10-27T07:00:00+0000"
         "UPPER_BOUND": "2024-10-27T07:00:00+0000"
         "NUM_PARTITIONS": "10"
     }
```

# Facebook Page Insights connection options


The following are connection options for Facebook Page Insights:
+ `ENTITY_NAME`(String) - (Required) Used for Read. The name of your object in Facebook Page Insights.
+ `API_VERSION`(String) - (Required) Used for Read. Facebook Page Insights Rest API version you want to use.
+ `SELECTED_FIELDS`(List<String>) - Default: empty(SELECT \$1). Used for Read. Columns you want to select for the object.
+ `FILTER_PREDICATE`(String) - Default: empty. Used for Read. It should be in the Spark SQL format.
+ `QUERY`(String) - Default: empty. Used for Read. Full Spark SQL query.
+ `PARTITION_FIELD`(String) - Used for Read. Field to be used to partition query.
+ `LOWER_BOUND`(String)- Used for Read. An inclusive lower bound value of the chosen partition field.
+ `UPPER_BOUND`(String) - Used for Read. An exclusive upper bound value of the chosen partition field.
+ `NUM_PARTITIONS`(Integer) - Default: 1. Used for Read. Number of partitions for read.
+ `INSTANCE_URL`(String) - (Required) Used for Read. A valid Facebook Page Insights instance URL.

# Limitations and notes for Facebook Page Insights connector


The following are limitations or notes for the Facebook Page Insights connector:
+ Most metrics will update once every 24 hours.
+ Only the last two years of insights data is available.
+ Only 90 days of insights can be viewed at one time when using the `since` and `until` parameters.