Ethan
Ethan

Reputation: 2087

AWS Node.js Lambda HTTPS Request Not Sending Data To Facebook Conversions API

I am attempting to send data to Facebook Conversion API via an AWS Lambda function written in node.js. Documentation here.

All data is passed successfully to the function via API gateway and external GET requests.

However when the function executes, there's no indication the data was actually sent, as there is no response logged from Facebook.

The current code is as follows:

const https = require('https');

let path = '/v15.0/' + pixelId +'/events?access_token='+ access_token;
            var options = {
                'method': 'POST',
                'hostname': 'graph.facebook.com',
                'path': path
              ,
              'headers': {
                'port': '443',
                'Content-Type': 'application/json'
              }};
            
              var body = [{
                "data": [
                    {
                        "event_name": conversionEvent,
                        "event_time": current_timestamp,
                        "action_source": "website",
                        "event_source_url": site,
                        "event_id": current_timestamp * Math.floor(Math.random() * 10),
                        "user_data": {
                            "em": email,
                            "phone": phone,
                            "fn": fName,
                            "ln": lName,
                            "client_ip_address": userIp,
                            "st": userState,
                            "zp": userZip,
                            "ct": userCity,
                            "fbc": clickId
                        }
                    }
                ]
                }];

                console.log(JSON.stringify(body));
                
                var req = https.request(options, function (res) {
                    var chunks = [];
                  
                    res.on("data", function (chunk) {
                        chunks.push(chunk);
                    });
                  
                    res.on("end", function (chunk) {
                     var body = Buffer.concat(chunks);
                     console.log(body.toString());
                     console.log("END");
                      response.statusCode = 200;
                      response.body = JSON.stringify("Success");
                    });
                  
                    res.on("error", function (error) {
                      console.error(error);
                      response.body = JSON.stringify("Error Posting Data to Facebook " + JSON.stringify(error));
                    })
                  });

                  req.write(JSON.stringify(body));
        
                  req.end();

I've also modified the lambda function to timeout at 30 seconds, rather than 3. Also note the function is NOT operating within a private VPC.

Here's a screenshot of the log output: enter image description here

Any guidance would be greatly appreciated.

Upvotes: 0

Views: 182

Answers (1)

Sh_gosha
Sh_gosha

Reputation: 111

Try this:

const https = require('https');

exports.handler = async (event) => {

    let path = '/v15.0/' + pixelId +'/events?access_token='+ access_token;
                var options = {
                    'method': 'POST',
                    'hostname': 'graph.facebook.com',
                    'path': path
                  ,
                  'headers': {
                    'port': '443',
                    'Content-Type': 'application/json'
                  }};
                
                  var body = [{
                    "data": [
                        {
                            "event_name": conversionEvent,
                            "event_time": current_timestamp,
                            "action_source": "website",
                            "event_source_url": site,
                            "event_id": current_timestamp * Math.floor(Math.random() * 10),
                            "user_data": {
                                "em": email,
                                "phone": phone,
                                "fn": fName,
                                "ln": l

Upvotes: 0

Related Questions