Search code examples
javascriptarraysfacebook-graph-api

Access the wanted field parameters from the output of facebook ads graph api


I've installed Facebook Pixel on my website, and it records purchases made on my site. The script on my site is the normal purchase standard event:

Right now, my query looks like this:

{
  "data": [
    {
      "actions": [
        {
          "action_type": "link_click",
          "value": 19
        },
        {
          "action_type": "offsite_conversion.fb_pixel_purchase",
          "value": 1
        },
        {
          "action_type": "offsite_conversion.fb_pixel_view_content",
          "value": 19
        },
        {
          "action_type": "post_like",
          "value": 88
        },
        {
          "action_type": "page_engagement",
          "value": 107
        },
        {
          "action_type": "post_engagement",
          "value": 107
        },
        {
          "action_type": "offsite_conversion",
          "value": 20
        }
      ],

As you can see, the query returns a lot of parameters that i don't want to track. There are some fields that i only want to record, i.e link_click & post_like. Rest other parameters, i want to remove them.

Is there any way to get only some required fields rather than a bunch of metrics

I tried using specific metrics mentioned in the Facebook API parameters. Like actions.purchase_conversion_value, but the output is no such parameters found.


Solution

  • Solution 1: If you get this data from API, then you must subscribe to get data. So you can filter your needed data yourself using RXJS operators specially filter. If we consume data as actions list Then the code will be like this:

    this.api
      .pipe(
        map((actions) => actions.filter(
          (action) => ['link_click', 'post_like'].includes(action.action_type))
        )
      )
      .subscribe(
        data => console.log(data)
      )
    

    And the output will be:

    [{
        "action_type": "link_click",
        "value": 19
    },
    {
        "action_type": "post_like",
        "value": 88
    }]
    

    Solution 2: Or if you have just the data without subscribing, again you can filter it by javascript filter function like this:

    let result = this.data.filter((action) => ['link_click', 'post_like'].includes(action.action_type));
    

    Result will be the same