Facebook pixel events call from server

Use Offline Conversions to record events that happen after a user has left your website. Logging these conversions, technically, is very easy. Setting everything up takes a little effort

tldr; check the code below


Follow setup steps in the FB docs (Setup steps 1-5) which are:

  • Setup facebook Business Manager account
  • Add a new app to Business Manager account
  • Create an Ad account, if you don't already have one
  • Create a System User for the ad account

After the setup, follow Upload Event Data steps on the same page, steps 1-3 to create an offline event set and associate it with your ad. These can be carried out in the Graph API Explorer by following the links in the examples. These can be done programmatically, but is out of the scope of making the event calls from the server for one campaign.

Once you have created the event set, then you can upload your CompleteRegistration events!

You will need to make a multipart form data request to FB, the data key will be an array of your conversion events. As @Cbroe mentioned, you must hash your match keys (the data you have available about your user to match them with a FB user) before sending to FB. The more match keys you are able to provide, the better chance at matching your user. So if you can get their email and phone at the same time, you're much more likely to match your user.

Here's an example of the call to FB using node.js:

    var request = require('request')
    
    // The access token you generated for your system user 
    var access_token = 'your_access_token'
    
    // The ID of the conversion set you created 
    var conversionId = 'your_conversion_set_id'
    
    var options = {
        url: 'https://graph.facebook.com/v2.12/' + conversionId + '/events',
        formData: {
            access_token: access_token,
            upload_tag: 'registrations', //optional 
            data: [{
                match_keys: {
                    "phone": ["<HASH>", "<HASH>"]
                },    
                currency: "USD",
                event_name: "CompleteRegistration",
                event_time: 1456870902,
                custom_data: { // optional 
                    event_source: "manager approved"
                },
            }]
        }
    }
    
    request(options, function(err, result) {
        // error handle and check for success
    })

Offline Conversion Docs


Facebook has now a Server-Side API: https://developers.facebook.com/docs/marketing-api/server-side-api/get-started

Implementing this is similar to implementing the offline events outlined in the accepted answer.

Keep in mind that it will always be cumbersome to track and connect events from the browser and from your server. You need to share a unique user id between the browser and server, so that Facebook (or any other analytics provider) will know that the event belongs to the same user.

Tools like mixpanel.com and amplitude.com may be more tailored to your needs, but will get very expensive once you move out of the free tier (100+ EUR at mixpanel, 1000+ EUR at Amplitude, monthly). Those tools are tailored towards company success, whereas Facebook is tailored towards selling and measuring Facebook ads.