SHARE
TWEET

Untitled

a guest Jun 16th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. composer require googleads/googleads-php-lib
  2.      
  3. <?php namespace AppConsoleCommandsGoogle;
  4.  
  5. use AppModelAdvExpense;
  6. use AppModelCampaign;
  7. use AppModelSource;
  8. use AppServiceXml;
  9. use CarbonCarbon;
  10. use GoogleAdsApiAdWordsAdWordsServices;
  11. use GoogleAdsApiAdWordsAdWordsSession;
  12. use GoogleAdsApiAdWordsAdWordsSessionBuilder;
  13. use GoogleAdsApiAdWordsReportingv201809DownloadFormat;
  14. use GoogleAdsApiAdWordsReportingv201809ReportDefinition;
  15. use GoogleAdsApiAdWordsReportingv201809ReportDefinitionDateRangeType;
  16. use GoogleAdsApiAdWordsReportingv201809ReportDownloader;
  17. use GoogleAdsApiAdWordsv201809cmCampaignService;
  18. use GoogleAdsApiAdWordsv201809cmDateRange;
  19. use GoogleAdsApiAdWordsv201809cmOrderBy;
  20. use GoogleAdsApiAdWordsv201809cmPaging;
  21. use GoogleAdsApiAdWordsv201809cmReportDefinitionReportType;
  22. use GoogleAdsApiAdWordsv201809cmSelector;
  23. use GoogleAdsApiAdWordsv201809cmSortOrder;
  24. use GoogleAdsApiCommonOAuth2TokenBuilder;
  25. use IlluminateConsoleCommand;
  26.  
  27. class Expenses extends Command
  28. {
  29.     /**
  30.      * The name and signature of the console command
  31.      *
  32.      * @var string
  33.      */
  34.     protected $signature = 'google:expenses';
  35.     /**
  36.      * The console command name.
  37.      *
  38.      * @var string
  39.      */
  40.     protected $name = 'Import Google Ads Expenses';
  41.     /**
  42.      * The console command description.
  43.      *
  44.      * @var string
  45.      */
  46.     protected $description = "Import Google Ads Expenses";
  47.  
  48.     public function handle()
  49.     {
  50.         $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile(storage_path('adsapi_php.ini'))->build();
  51.         $session = (new AdWordsSessionBuilder())->fromFile(storage_path('adsapi_php.ini'))->withOAuth2Credential($oAuth2Credential)->build();
  52.  
  53.         $date = Carbon::yesterday();
  54.  
  55.         $this->saveExpenses($session, $date);
  56.     }
  57.  
  58.     public function saveExpenses($session, $date)
  59.     {
  60. // Create selector.
  61.         $selector = new Selector();
  62.         $selector->setFields(['CampaignId', 'CampaignName', 'Clicks', 'Cost']);
  63.         $selector->setDateRange(new DateRange(
  64.             $date->format('Ymd'),
  65.             $date->format('Ymd')
  66.         ));
  67.  
  68. // Create report definition.
  69.         $reportDefinition = new ReportDefinition();
  70.         $reportDefinition->setSelector($selector);
  71.         $reportDefinition->setReportName('Expenses report ' . uniqid());
  72.         $reportDefinition->setDateRangeType(ReportDefinitionDateRangeType::CUSTOM_DATE);
  73.         $reportDefinition->setReportType(ReportDefinitionReportType::CAMPAIGN_PERFORMANCE_REPORT);
  74.         $reportDefinition->setDownloadFormat(DownloadFormat::XML);
  75.  
  76. // Download report.
  77.         $reportDownloader = new ReportDownloader($session);
  78.         $reportDownloadResult = $reportDownloader->downloadReport($reportDefinition);
  79.         $result = $reportDownloadResult->getAsString();
  80.  
  81.         $report = Xml::namespacedXMLToArray($result);
  82.         foreach ($report['table']['row'] as $item) {
  83.             $campaign_id = $item['@attributes']['campaignID'];
  84.             $campaign_name = $item['@attributes']['campaign'];
  85.             $cost = $item['@attributes']['cost'];
  86.  
  87.             if ($cost > 0) {
  88.                 $dbCampaign = Campaign::where('code', $campaign_id)->get()->first();
  89.  
  90.                 if ($dbCampaign) {
  91.                     $price = round($cost / 1000000);
  92.                     if ($price) {
  93.                         $dbCost = AdvExpense::updateOrCreate(
  94.                             [
  95.                                 'campaign_id' => $dbCampaign->id,
  96.                                 'date' => $date,
  97.                             ],
  98.                             [
  99.                                 'price' => $price,
  100.                             ]
  101.                         );
  102.  
  103.                         if ($dbCost->exists) {
  104.                             $this->info($dbCampaign->name . ' - ' . $date->format('Y-m-d') . ' - ' . $price . ' OK');
  105.                         } else {
  106.                             $this->error($dbCampaign->name . ' - ' . $date->format('Y-m-d') . ' - ' . $price . ' ERROR');
  107.                         }
  108.                     }
  109.                 } else {
  110.                     $this->error('Campaign ' . $campaign_name . ' not found!');
  111.                 }
  112.             }
  113.         }
  114.     }
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top