Monday, March 14, 2022

[FIXED] How can I get the amount of clicks per ad using facebooks marketing API php SDK?

Issue

I want to print out the name of my ads and the amount of clicks they have gotten. All my ads are in the $ads array.

foreach ($ads as $ad){
    $adinsights = $ad->getInsights( array (
        AdsInsightsFields::INLINE_LINK_CLICKS
        ));
    echo $ad->{AdFields::NAME}.PHP_EOL;
    echo $adinsights->{AdsInsightsFields::INLINE_LINK_CLICKS}.PHP_EOL;
}

The code above only prints out the names of the ads.

$adinsights->{AdsInsightsFields::INLINE_LINK_CLICKS}.PHP_EOL resolves into an empty string ""

What am I doing wrong?


Solution

This function returns a collection of AdInsight objects, so you're going to have to iterate them to get the output:

foreach ($ads as $ad){
  $adinsights = $ad->getInsights( array (
    AdsInsightsFields::INLINE_LINK_CLICKS
  ));

  foreach($adinsights as $a){
    echo $a->date_start . ' - ' . $a->date_stop . ': ' . $a->inline_link_clicks.PHP_EOL;
  }

}


Answered By - Paul Bain

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.