Facebook pixel events call from server

后端 未结 2 799
忘了有多久
忘了有多久 2020-12-28 08:20

I have absolutelly the same question as dan here - Facebook conversion pixel with "server to server" option . There was written, that there was no way, but it was

2条回答
  •  不思量自难忘°
    2020-12-28 09:01

    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": ["", ""]
                },    
                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

提交回复
热议问题