PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, March 14, 2022

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

 March 14, 2022     facebook, facebook-marketing-api, facebook-php-sdk, php     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing