Facebook Conversion API Project

Hey everyone :wave::wave::wave:

I’m working on this workflow to send purchase events to Facebook Conversion API :fire:.
Here’s a screenshot on how it looks so far.

Let me explain the steps:

  1. Google Sheet looks for new rows added with data every two minutes (new purchase).

  1. Set node filter data to keep data needed for Facebook into 1 item

  1. The code node separates data into multiple items.

  1. Crypto node adds a HashVal property for all those items (to hash data to pass it to Facebook Conversion API).

  1. Item list node splits all that data againg into one item.

  1. Set node organizes data in the format that Facebook needs it.

  1. HTTP request node sends the Purchase event to Facebook Conversion API.

Now. this works. But only when I have just one purchase in the pipeline.

If I receive two purchases in my store, and the Google Sheets record two items to pass to the workflow, I’m not going to be able to perform the steps after the code node because the code node is set to run once for all items.
The code node separates items into multiple items so that the Crypto node can add a ‘hashVal’ to each of those items.

And here’s it’s where it gets tricky…

If I receive two purchases, meaning two items inside the two minutes time window, the code node will only pass one of those items. So, I will lose track of the other purchases.

Not good…

This is the code that I’m using in the code node

const res = []

const item = items[0].json 



for (let key in item) {

  res.push({json:{propKey:key,propVal:item[key]}})

}



return res

It’s returning something like this:

[
{
"propKey": "eventTime",
"propVal": "1678286434"
},
{
"propKey": "name",
"propVal": "Sammy Harri"
},
{
"propKey": "email",
"propVal": "[email protected]"
},
{
"propKey": "telefono",
"propVal": "296415616557"
},
{
"propKey": "extid",
"propVal":"767145"
},
{
"propKey":"city",
"propVal":"La Plata"
},
{
"propKey":"countrycode",
"propVal":"AR"
},
{
"propKey":"region",
"propVal":"Buenos Aires"
},
{
"propKey":"statecode",
"propVal":"B"
},
{
"propKey":"content",
"propVal":"false"
}
]

I did try running this code with the code node set up to “Run Once for each item”. But I’m receiving this error.

ERROR: items is not defined [line 3, for item 0]

In a 2nd scenario, I did also try using this code snippet

const res = []

for (const item of items) {
  const json = item.json;
  for (let key in json) {
    const obj = { propKey: key, propVal: json[key] };
    res.push({ json: obj });
  }
}

return res;

In this 2nd case, with the code node set to Run once for all items works fine.

The downside is that I now have a bunch of items to organize later on in the workflow (…and actually, I dont really find a way to do it… lol)

This is how it looks like the output of that code node running that script. In this example, I fed the code node with two items (two purchases), each of those including 14 different values.

As a result, I receive 28 items to organize.

So, I have two questions here:

  1. Is there any possibility to iterate the first version of the code to run for once for each item? I’m assuming that the output of that script will allow me to add the ‘hashVal’ to each key later in the workflow.

  2. Given the second case, how can I organize the output data into two items (data 1, data 2) that include their unique keys, values, and hash values so that I can create multiple items to send to the Conversion API?

In the 2nd example, I will eventually need to organize data this way. (after the crypto node stage)

[
  {
    "data 1 ": [
      {
        "propKey": "eventTime",
        "propVal": "1678286434",
        "hashVal": "256bad6e54c5c4404ca402833472454c4062e0d3f7088fc0e474ab6946314eec"
      },
      {
        "propKey": "name",
        "propVal": "Sammy Harri",
        "hashVal": "326ddf9435ee83a92b53c2bff407f1982606edb626669ff465853cf9d5fc1b13"
      },
      {
        "propKey": "email",
        "propVal": "[email protected]",
        "hashVal": "c1eb34440744cad6f19aa22254990d128f76041f9145eabb384a89777811ba61"
      },
      {
        "propKey": "telefono",
        "propVal": "296415616557",
        "hashVal": "c9f96a8a5d5b750b51a9d3fc84148a1eda1c9874606299ad76105216b2d298bc"
      },
      {
        "propKey": "extid",
        "propVal": "767145",
        "hashVal": "7046f6b6c2108a604ee45e76d21c36f644e09b86bb847fef12c7df310b74dc7e"
      },
      {
        "propKey": "city",
        "propVal": "La Plata",
        "hashVal": "9c474d42b11792e70904ac98f88050b4e0f31ad29fb884bd746f236941a7bc40"
      },
      {
        "propKey": "countrycode",
        "propVal": "AR",
        "hashVal": "b1b7afa76db271451a1f3fff738ff21ed53cca91f7f580bb294193e9d2da31b4"
      },
      {
        "propKey": "region",
        "propVal": "Buenos Aires",
        "hashVal": "abae768240354770b732e87de24a76aa3123c0abbe7f4ebe6110c090d30eae98"
      },
      {
        "propKey": "statecode",
        "propVal": "B",
        "hashVal": "df7e70e5021544f4834bbee64a9e3789febc4be81470df629cad6ddb03320a5c"
      },
      {
        "propKey": "content",
        "propVal": "false",
        "hashVal": "fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa"
      },
      {
        "propKey": "ip",
        "propVal": "191.83.218.36",
        "hashVal": "3544b212bd1f5bea41f57890dca1d7ab278f4a218adb57526cecd5c79c9f11d7"
      },
      {
        "propKey": "fbp",
        "propVal": "fb.1.1675740572292.2116826590",
        "hashVal": "d1115cb8e333fe40b1e7c6232eaa88784759b8b5442e3ece8b273e626be0122e"
      },
      {
        "propKey": "fbc",
        "propVal": "fb.1.1678287438814.IwAR00D2ywLcwToZb0JkYLay6ZBuiTPV6aYHKRy-RAkmjUQfySlaE1t1JRl2c",
        "hashVal": "76c55428c472790addac4dc220808a5cfb097d4bee712e44759cfb541af7bbf8"
      },
      {
        "propKey": "eventid",
        "propVal": "799888_888887_enter",
        "hashVal": "6c5516e8c244284e951718731cdd0b800c1b3d4b46ce53d491ecdbd262b3e3ef"
      }
    ]
  }

  {
    "data 2 ": [
  
      {
        "propKey": "eventTime",
        "propVal": "1678285834",
        "hashVal": "cf741851074e32ff69206ba08f4faf29f8bb6e63a7105da292f89c9fefe829e7"
      },
      {
        "propKey": "name",
        "propVal": "Don joe",
        "hashVal": "f251179fa148cfb94d50c2e7ac361c37c794e6628fa147003071e9c1ce1b8156"
      },
      {
        "propKey": "email",
        "propVal": "[email protected]",
        "hashVal": "360cbed9cc7ed697f9975723187c1d9bec5a22cc8d71953c98fdfd5c6b64b34f"
      },
      {
        "propKey": "telefono",
        "propVal": "296615616782",
        "hashVal": "6d507b46c5341528bff2094a104969f8fc65bfe5c151442567a7961887a24831"
      },
      {
        "propKey": "extid",
        "propVal": "763845",
        "hashVal": "6fd08a96de8087309dd4a8b0c7c924fa8345bb9136b88ab6c102a69a7275ed80"
      },
      {
        "propKey": "city",
        "propVal": "La Plata",
        "hashVal": "9c474d42b11792e70904ac98f88050b4e0f31ad29fb884bd746f236941a7bc40"
      },
      {
        "propKey": "countrycode",
        "propVal": "AR",
        "hashVal": "b1b7afa76db271451a1f3fff738ff21ed53cca91f7f580bb294193e9d2da31b4"
      },
      {
        "propKey": "region",
        "propVal": "Buenos Aires",
        "hashVal": "abae768240354770b732e87de24a76aa3123c0abbe7f4ebe6110c090d30eae98"
      },
      {
        "propKey": "statecode",
        "propVal": "B",
        "hashVal": "df7e70e5021544f4834bbee64a9e3789febc4be81470df629cad6ddb03320a5c"
      },
      {
        "propKey": "content",
        "propVal": "false",
        "hashVal": "fcbcf165908dd18a9e49f7ff27810176db8e9f63b4352213741664245224f8aa"
      },
      {
        "propKey": "ip",
        "propVal": "191.83.218.36",
        "hashVal": "3544b212bd1f5bea41f57890dca1d7ab278f4a218adb57526cecd5c79c9f11d7"
      },
      {
        "propKey": "fbp",
        "propVal": "fb.1.1675740572292.2116826590",
        "hashVal": "d1115cb8e333fe40b1e7c6232eaa88784759b8b5442e3ece8b273e626be0122e"
      },
      {
        "propKey": "fbc",
        "propVal": "fb.1.1678286573688.IwAR0_vlFzIHydDfi4Hvo1nqRgQSoDMxO-JCRbSjK0-aiZ6vAaobshGhNO7uM",
        "hashVal": "3f6e3c793181e29aeaa4150e52bf0904eb1d2e2bdbc9fac0621f6a37a57ddce7"
      },
      {
        "propKey": "eventid",
        "propVal": "799888_859217_enter",
        "hashVal": "d01b1cb9057fae79cc0959dc03a8e0c4c1ca0053069a867bcea97f9fcbc406ee"
      }
    ]
  }

]

Thanks, everyone!

cheers!

1 Like

Hey @LucasOneSouth,

I suspect the issue here is the code node uses slightly different syntax to get all items than the older function and function items nodes so if the code is taken from an older post it would need to be updated.

So rather than using items you would need to use $input.all() instead but if you just want the first item you can use $input.first() this would apply only to Run once for all, If you are using Run once for each item you would then need to use $input.item.json to use the data for the item as it is already doing the loop internally.

Hopefully this helps get you to the next stage :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.