Campaign Stats using PHP

483 views
Skip to first unread message

JS

unread,
Jan 18, 2010, 3:52:21 PM1/18/10
to AdWords API Forum
Hi ,
I am new to PHP and Adwords API. Can someone please post an example
(using PHP) on how to get the CampaignStats given a Campaign ID .I can
get it for all campaigns, but not sure how to use the Selector for
Active Campaigns. Please see code snippet below:-

$campaignService = $user->GetCampaignService('v200909');
$today = date('Y-m-d');

$start = mktime(0,0,0,date("m"),date("d")-15,date("Y"));
$end = mktime(0,0,0,date("m"),date("d")-1,date("Y"));

$start_date = date("Y-m-d", $start);
$end_date = date("Y-m-d", $end);

$campaignId = <insert_campaign_ID_here>;

// Create selector.
$selector = new CampaignSelector();
$selector->ids = array($campaignId);
$page = $campaignService->get($selector);

//Not sure how to use the selector here
$campstats = new Stats();
$campstats->start_date = $start_date;
$campstats->end_date = $end_date;

//Not sure how to use the selector here
$page = $campaignService->get($campstats);

if (isset($page->entries)) {
foreach ($page->entries as $campaign) {
echo 'Campaign ID:' . $campaign->id . ' Campaign name:' . $campaign-
>name .' Campaign startdate:"' . $campaign->startDate."<br>";
echo 'Campaign Clicks:' . $campaign->stats->clicks ."<br>"; //this
is empty at present
echo 'Campaign cost:' . $campaign->stats->cost ."<br>"; //this is
empty at present

thanks and any info is appreciated..
-Jeya

AdWords API Advisor

unread,
Jan 20, 2010, 12:33:17 PM1/20/10
to AdWords API Forum
Hi Jeya,

All calls made to CampaignService->get() must be made with a
CampaignSelector object:

http://code.google.com/apis/adwords/v2009/docs/reference/CampaignService.CampaignSelector.html

To retrieve stats, set the "statsSelector" field of the
CampaignSelector to a new StatsSelector object:

http://code.google.com/apis/adwords/v2009/docs/reference/CampaignService.StatsSelector.html

After the selector is configured, run the CampaignService->get() with
the CampaignSelector. For example:

// Create selector.
$selector = new CampaignSelector();
$selector->ids = array($campaignId);

// Create stats selector.
$statsSelector = new StatsSelector();
$dateRange = new DateRange();
$dateRange->min = $start_date;
$dateRange->max = $end_date;
$statsSelector->dateRange = $dateRange;
$selector->statsSelector = $statsSelector;

// Get campaigns with stats.
$page = $campaignService->get($selector);

Best,
- Eric Koleda, AdWords API Team

JS

unread,
Jan 20, 2010, 1:29:16 PM1/20/10
to AdWords API Forum
Thanks Eric,
works great !!.

-Jeya

On Jan 20, 9:33 am, AdWords API Advisor <adwordsapiadvi...@google.com>
wrote:


> Hi Jeya,
>
> All calls made to CampaignService->get() must be made with a
> CampaignSelector object:
>

>  http://code.google.com/apis/adwords/v2009/docs/reference/CampaignServ...


>
> To retrieve stats, set the "statsSelector" field of the
> CampaignSelector to a new StatsSelector object:
>

>  http://code.google.com/apis/adwords/v2009/docs/reference/CampaignServ...

ipsita rout

unread,
Jan 22, 2010, 8:20:03 AM1/22/10
to AdWords API Forum
Hi Jeya
This is Ipsita
I have done the exact thing u asked for.Before using the below code
you should keep your account information in 'Account_info.php' file.
require_once('soapclientfactory.php');
require_once('Account_Info.php');

$campaign_service = SoapClientFactory::GetClient($namespace . '/
CampaignService?wsdl', 'wsdl');
$campaign_service->setHeaders($headers);

$request_xml = '<getAllAdWordsCampaigns><dummy>0</dummy></
getAllAdWordsCampaigns>';
$campaigns = $campaign_service->call('getAllAdWordsCampaigns',
$request_xml);
$campaigns = $campaigns['getAllAdWordsCampaignsReturn'];

$start_date = date("Y-m-d");//'2009-09-20';exit;
$end_date = $start_date;
//U can mention both the start date and end date according to ur
choice

$count = count($campaigns);
for ($i = 0; $i < $count; $i++) {
$campaignIds[] = $campaigns[$i]['id'];
$camp_name[] = $campaigns[$i]['name'];
$xml[] = "<getCampaignStats><campaignIds>".$campaignIds[$i]."</
campaignIds>
<startDay>".$start_date."</startDay>
<endDay>".$end_date."</endDay>
</getCampaignStats>";
$campaign_stats[$i] = $campaign_service->call('getCampaignStats',$xml
[$i]);
$campaign_stats[$i] = $campaign_stats[$i]['getCampaignStatsReturn'];

print "'".$start_date."','".$campaign_stats[$i]['impressions']."','".
$campaign_stats[$i]['clicks']."','".round($campaign_stats[$i]['cost']/
1000000,2)."','".$camp_name[$i]."'+";

Reply all
Reply to author
Forward
0 new messages