App Events for Websites (Beta)

分类目录: 应用事件

App Events for Websites (Beta)

You can use app events on your website to better understand the actions people take and to see powerful, aggregated demographics in Facebook Analytics for Apps.

You can log events from your website using the Facebook SDK for JavaScript. You can log any of the predefined events we provide, such as 'purchase', 'add to cart', 'level achieved' and more. You can also define new custom events that represent meaningful actions your customers take.

To get started, use this quickstart guide for Analytics for Apps:

Analytics for Apps Quickstart

Implementing app events on your website

Integrate the Facebook JavaScript SDK with your website.

JavaScript SDK Quickstart Guide

After you've integrated the JavaScript SDK, then log the Page View event on every page of your website to ensure you see accurate user counts and session time metrics in Analytics for Apps.

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'your-app-id',
      xfbml      : true,
      version    : 'v2.7'
    });
    FB.AppEvents.logPageView();
  };

  (function(d, s, id){
     var js, fjs = d.getElementsByTagName(s)[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement(s); js.id = id;
     js.src = "//connect.facebook.net/en_US/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>
);

Log events with parameters

To get the most out of Analytics for Apps, we recommend logging more events and adding parameters to them. You can use our pre-defined events or you can create custom events that you define. You can also log additional parameters with your events. For example, you can log a purchase event with a parameter that describes the value of the purchase.

Following is an example of how you can log an event with a parameter that describes it:

var params = {};
params[FB.AppEvents.ParameterNames.LEVEL] = '12'; //player level
FB.AppEvents.logEvent(
  FB.AppEvents.EventNames.ACHIEVED_LEVEL,
  null,  // numeric value for this event - in this case, none
  params
);

You can have a maximum of 1,000 different event names. Note that no new event types will be logged if you reach this maximum level. If you exceed this limit, you'll see an 100 Invalid parameter error when trying to log new event types. However, you can deactivate any existing events that you may not be using. Learn more about event limits.

Log purchase events

The Facebook SDK includes a dedicated function for logging purchases, which requires the specification of a currency.

  var params = {};
  params[FB.AppEvents.ParameterNames.CONTENT_ID] = 'QW-12345';
  FB.AppEvents.logPurchase(98.76, 'USD', params);

Note: to use the pre-defined parameters, you have to create the params object first, then pass it to the function:

  var params = {};
  params[FB.AppEvents.ParameterNames.CONTENT_ID] = '12345';
  FB.AppEvents.logPurchase(98.76, 'USD', params);

Using custom app events

You can also create your own custom events by specifying their name as a string:

FB.AppEvents.logEvent('battledAnOrc');

The maximum length for a custom event name is 40 characters and it can only use characters that are letters, numbers, underscores or dashes.

Catching exceptions

Note that if you call an SDK function with incorrect parameters, it will throw an exception. Make sure your code passes the correct parameters and catch any exceptions if they occur.

Access your App Events Report

  1. Go to Analytics for Apps: https://www.facebook.com/analytics
  2. Select your website.
  3. Go to the section Events Debugging.

Debug

To improve performance, the JavaScript SDK is minified when it's loaded. You can also load a debug version of the JavaScript SDK that's not minified and that includes more logging and stricter argument checking. To do this, change the js.src value in your loading code to match this:

js.src = "//connect.facebook.net/en_US/sdk/debug.js";

Reference

  • Javascript SDK
  • AppEvents.LogEvents Reference