如何通过Graph API或Facebook Web SDK获取特定URL的页面浏览量?
Great question! When you use window.FB.AppEvents.logPageView() to log page views, that data gets sent to Meta's event infrastructure (formerly Facebook Analytics). To programmatically fetch the page view count for a specific URL, you'll need to use the Graph API—the Facebook Web SDK doesn't include methods to query historical event data directly. Here's how to do it:
Prerequisites First
Before you start, make sure you have:
- Admin or Analytics access to the Facebook/Meta app associated with your page views.
- A valid access token: Use an app access token (best for server-side calls to avoid exposing sensitive permissions) or a user access token with
ads_readand/orbusiness_managementpermissions.
Option 1: Query Raw Page View Events (Graph API)
If you want to pull individual page view events filtered by your target URL, use the /{app-id}/app_events endpoint. This lets you fetch granular event data matching your criteria.
Example Request
GET /{your-app-id}/app_events? event=page_view& fields=id,created_time,parameters& filter=[{"field":"url","operator":"EQUAL","value":"https://your-target-url.com"}]& access_token={your-access-token}
Breakdown of Parameters:
event=page_view: Filters results to only include page view events.fields: Specifies which data points to return (here, event ID, timestamp, and the parameters sent with the event).filter: Narrows results to only events where theurlparameter matches your target URL. Note: If you passed a custom URL vialogPageView({url: "custom-url"}), adjust thefieldvalue to match your custom parameter key.
Option 2: Get Aggregated Page View Metrics (Insights API)
If you want a summarized count of page views per URL (instead of individual events), use the /{app-id}/insights endpoint. This returns aggregated metrics, which is ideal for reporting.
Example Request
GET /{your-app-id}/insights? metric=page_views& breakdown=url& period=day& since=2024-01-01& until=2024-01-31& access_token={your-access-token}
Breakdown of Parameters:
metric=page_views: Targets the page view count metric.breakdown=url: Splits the metric by page URL, so you get a count for each unique URL.period: Defines the time range (useday,week,month, orlifetime).since/until: Narrows the results to a specific date range.
Key Notes
- Data Delay: Meta's event data isn't real-time—expect a delay of 15 minutes to a few hours before new page views show up in API results.
- Frontend vs. Server-Side: Always make Graph API calls from your server, not the frontend. Exposing access tokens in client-side code risks unauthorized access to your app's data.
- Custom Parameters: If you customized the
logPageView()call with additional parameters (e.g.,window.FB.AppEvents.logPageView({content_url: "your-url"})), adjust the filter or breakdown field to match your custom parameter name.
内容的提问来源于stack exchange,提问作者Mike Ng

