Skip to content

Commit

Permalink
Add facebook importer
Browse files Browse the repository at this point in the history
  • Loading branch information
danielstonies committed Dec 8, 2020
1 parent d6629db commit dba7760
Showing 1 changed file with 48 additions and 106 deletions.
154 changes: 48 additions & 106 deletions Platforms/FacebookAds/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,21 @@
*/
namespace Piwik\Plugins\AOM\Platforms\FacebookAds;

use DateTime;
use Exception;
use FacebookAds\Api;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdsInsightsFields;
use FacebookAds\Object\Values\AdsInsightsLevelValues;
use Monolog\Logger;
use Piwik\Db;
use Piwik\Plugins\AOM\AOM;
use Piwik\Plugins\AOM\Platforms\AbstractImporter;
use Piwik\Plugins\AOM\Platforms\ImporterInterface;
use Piwik\Plugins\AOM\Services\DatabaseHelperService;
use Piwik\Plugins\AOM\SystemSettings;

use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;


class Importer extends AbstractImporter implements ImporterInterface
{

/**
* @var array
*/
protected $adsInsightsFields = [
AdsInsightsFields::DATE_START,
AdsInsightsFields::ACCOUNT_NAME,
AdsInsightsFields::ACCOUNT_ID,
AdsInsightsFields::CAMPAIGN_ID,
AdsInsightsFields::CAMPAIGN_NAME,
AdsInsightsFields::ADSET_ID,
AdsInsightsFields::ADSET_NAME,
AdsInsightsFields::AD_NAME,
AdsInsightsFields::AD_ID,
AdsInsightsFields::IMPRESSIONS,
AdsInsightsFields::CLICKS,
AdsInsightsFields::INLINE_LINK_CLICKS,
AdsInsightsFields::SPEND,
];

/**
* Imports all active accounts day by day
*/
Expand All @@ -66,102 +42,68 @@ public function import()
}

/**
* @param string $accountId
* @param array $account
* @param string $date
* @throws \Exception
*/
private function importAccount($account, $date)
{
$this->log(Logger::INFO, 'Will import FacebookAds account ' . $accountId. ' for date ' . $date . ' now.');

// var_dump($account);

// var_dump([$account['clientId'], $account['clientSecret'], $account['accessToken']]);
$api = Api::init($account['clientId'], $account['clientSecret'], $account['accessToken']);

$adAccount = new AdAccount($account['accountId'], null, $api);


foreach ($this->fetchAdsInsights($adAccount, $date) as $insight) {
var_dump([
$insight->{AdsInsightsFields::ACCOUNT_NAME},
$insight->{AdsInsightsFields::ACCOUNT_ID},
$insight->{AdsInsightsFields::CAMPAIGN_NAME},
$insight->{AdsInsightsFields::CAMPAIGN_ID},
$insight->{AdsInsightsFields::ADSET_NAME},
$insight->{AdsInsightsFields::ADSET_ID},
$insight->{AdsInsightsFields::AD_NAME},
$insight->{AdsInsightsFields::AD_ID},
$insight->{AdsInsightsFields::CLICKS},
$insight->{AdsInsightsFields::IMPRESSIONS},
$insight->{AdsInsightsFields::SPEND},
]
);
die();
// $ad = new FacebookAds();
// $ad
// ->setDate(new DateTime($date))
// ->setSaasUserId($user->getId())
// ->setAccountName($insight->{AdsInsightsFields::ACCOUNT_NAME})
// ->setAccountId($insight->{AdsInsightsFields::ACCOUNT_ID})
// ->setCampaignName($insight->{AdsInsightsFields::CAMPAIGN_NAME})
// ->setCampaignId($insight->{AdsInsightsFields::CAMPAIGN_ID})
// ->setAdsetName($insight->{AdsInsightsFields::ADSET_NAME})
// ->setAdSetId($insight->{AdsInsightsFields::ADSET_ID})
// ->setAdName($insight->{AdsInsightsFields::AD_NAME})
// ->setAdId($insight->{AdsInsightsFields::AD_ID})
// ->setClicks($insight->{AdsInsightsFields::CLICKS})
// ->setImpressions($insight->{AdsInsightsFields::IMPRESSIONS})
// ->setCosts($insight->{AdsInsightsFields::SPEND});
//
// $this->entityManager->persist($ad);
}



// $adAccount = new AdAccount($account['accountId']);


var_dump($adAccount->getData());
$accountId= $account['accountId'];

$cursor = $adAccount->getCampaigns();

// Loop over objects
foreach ($cursor as $campaign) {
echo $campaign->{CampaignFields::NAME}.PHP_EOL;
}
$this->log(Logger::INFO, 'Will import FacebookAds account ' . $account['accountId']. ' for date ' . $date . ' now.');

$api = Api::init($account['clientId'], $account['clientSecret'], $account['accessToken']);
$adAccount = new AdAccount('act_'.$accountId, null, $api);




// $this->deleteExistingData(AOM::PLATFORM_FACEBOOK_ADS, $accountId, $account['websiteId'], $date);

// throw new Exception('Not implemented');
}

/**
* @param AdAccount $adAccount
* @param string $date
* @return \FacebookAds\ApiRequest|\FacebookAds\Cursor|\FacebookAds\Http\ResponseInterface|null
*/
protected function fetchAdsInsights(AdAccount $adAccount, $date)
{
$insights = $adAccount->getInsights($this->adsInsightsFields, [
$this->deleteExistingData(AOM::PLATFORM_FACEBOOK_ADS, $accountId, $account['websiteId'], $date);

$insights = $adAccount->getInsights([
AdsInsightsFields::DATE_START,
AdsInsightsFields::ACCOUNT_NAME,
AdsInsightsFields::CAMPAIGN_ID,
AdsInsightsFields::CAMPAIGN_NAME,
AdsInsightsFields::ADSET_ID,
AdsInsightsFields::ADSET_NAME,
AdsInsightsFields::AD_NAME,
AdsInsightsFields::AD_ID,
AdsInsightsFields::IMPRESSIONS,
AdsInsightsFields::INLINE_LINK_CLICKS,
AdsInsightsFields::SPEND,
], [
'level' => AdsInsightsLevelValues::AD,
'time_range' => [
'since' => $date,
'until' => $date,
],
]);

$insights->setUseImplicitFetch(true);

return $insights;
foreach ($insights as $insight) {
Db::query(
'INSERT INTO ' . DatabaseHelperService::getTableNameByPlatformName(AOM::PLATFORM_FACEBOOK_ADS)
. ' (id_account_internal, idsite, date, account_id, account_name, campaign_id, campaign_name, '
. 'adset_id, adset_name, ad_id, ad_name, impressions, clicks, cost, ts_created) '
. 'VALUE (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())',
[
$accountId,
$account['websiteId'],
$insight->getData()[AdsInsightsFields::DATE_START],
$accountId,
$insight->getData()[AdsInsightsFields::ACCOUNT_NAME],
$insight->getData()[AdsInsightsFields::CAMPAIGN_ID],
$insight->getData()[AdsInsightsFields::CAMPAIGN_NAME],
$insight->getData()[AdsInsightsFields::ADSET_ID],
$insight->getData()[AdsInsightsFields::ADSET_NAME],
$insight->getData()[AdsInsightsFields::AD_ID],
$insight->getData()[AdsInsightsFields::AD_NAME],
$insight->getData()[AdsInsightsFields::IMPRESSIONS],
$insight->getData()[AdsInsightsFields::INLINE_LINK_CLICKS],
$insight->getData()[AdsInsightsFields::SPEND],
]
);
}
$this->log(Logger::INFO, 'Imported '.count($insights).' ads from ' . $account['accountId']. ' for date ' . $date . ' now.');
}


/**
* Convenience function for shorter logging statements
*
Expand Down

0 comments on commit dba7760

Please sign in to comment.