Campaign Performance Report using AWQL without downloading file in PHP

472 views
Skip to first unread message

jtre...@forcemktg.com

unread,
Jul 29, 2015, 9:00:17 AM7/29/15
to AdWords API Forum
So I have the following file which works great for using AWQL to get the data I need, but it puts it in csv file, which is highly unneccessary and inefficient since I dont need the file. Is there a way to simply have the data outputted to a variable like a SQL result variable which I can iterate through? Here is my current code:

<?php
/**
 * This example downloads a criteria report to a file.
 *
 * Copyright 2014, Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package    GoogleApiAdsAdWords
 * @subpackage v201506
 * @category   WebServices
 * @copyright  2014, Google Inc. All Rights Reserved.
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License,
 *             Version 2.0
 * @author     Danial Klimkin
 */

error_reporting(E_STRICT | E_ALL);

// Include the initialization file
require_once dirname(dirname(__FILE__)) . '/init.php';

require_once ADWORDS_UTIL_PATH . '/ReportUtils.php';

/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $filePath the path of the file to download the report to
 */
function DownloadCriteriaReportWithAwqlExample(AdWordsUser $user, $filePath,
    $reportFormat) {
  // Prepare a date range for the last week. Instead you can use 'LAST_7_DAYS'.
  $dateRange = sprintf('%d,%d',
      date('Ymd', strtotime('-7 day')), date('Ymd', strtotime('-1 day')));

  // Create report query.
  $reportQuery = 'SELECT CampaignId, CampaignName,  '
      . 'Impressions, Clicks, Cost FROM CAMPAIGN_PERFORMANCE_REPORT '
      . ' DURING ' . $dateRange;

  // Set additional options.
  $options = array('version' => ADWORDS_VERSION);

  // Optional: Set skipReportHeader, skipColumnHeader, skipReportSummary to
  //     suppress headers or summary rows.
  // $options['skipReportHeader'] = true;
  // $options['skipColumnHeader'] = true;
  // $options['skipReportSummary'] = true;
  // Optional: Set includeZeroImpressions to include zero impression rows in
  //     the report output.
  // $options['includeZeroImpressions'] = true;

  // Download report.
  ReportUtils::DownloadReportWithAwql($reportQuery, $filePath, $user,
      $reportFormat, $options);
$dataArray =file($filePath);
var_dump($dataArray);
  printf("Report was downloaded to '%s'.\n", $filePath);
}

// Don't run the example if the file is being included.
/*if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
  return;
}*/

try {
  // Get AdWordsUser from credentials in "../auth.ini"
  // relative to the AdWordsUser.php file's directory.
  $user = new AdWordsUser();

  // Log every SOAP XML request and response.
  $user->LogAll();

  // Download the report to a file in the same directory as the example.
  $filePath = dirname(__FILE__) . '/report.csv';
  $reportFormat = 'CSV';

  // Run the example.
  DownloadCriteriaReportWithAwqlExample($user, $filePath, $reportFormat);
} catch (Exception $e) {
  printf("An error has occurred: %s\n", $e->getMessage());
}

I'm currently using the php "file()" function, but this seems highly innedficient.

Anthony Madrigal

unread,
Jul 29, 2015, 12:20:49 PM7/29/15
to AdWords API Forum, jtre...@forcemktg.com, jtre...@forcemktg.com
Hello,

If you pass a null value to the ReportUtility constructor for $filePath, the report should then return as a string.

Regards,
Anthony
AdWords API Team
Reply all
Reply to author
Forward
0 new messages