Can someone help point me in the right direction as to why this code isn't working?
Essentially I want to create a csv, and whilst this script does this, the csv is blank. I have got a simple example working like this one:
Code: Select all
<?php
$sales = array (array('Northeast', '2005-01-01', '2005-02-01', 12.54),
array('Northwest', '2005-01-01', '2005-02-01', 546.33),
array('Southeast', '2005-01-01', '2005-02-01', 93.26),
array('Southwest', '2005-01-01', '2005-02-01', 945.21),
array('All Regions', '--', '--',1597.34) );
$fh = fopen('sales.csv', 'w') or die ("Can't open sales.csv");
foreach ($sales as $sales_line) {
if (fputcsv($fh, $sales_line) == false) {
die("Can't write CSV line");
}
}
fclose($fh) or die ("Can't close sales.csv");
echo "File 'sales_line.csv' written successfully";
?>
Code: Select all
define('ga_email','someone@gmail.com');
define('ga_password','apassword');
define('ga_profile_id','somenumbers');
require 'gapi.class.php';
$ga = new gapi(ga_email,ga_password);
$ga->requestReportData(ga_profile_id,array(
'date',
'dayOfWeek',
'campaign',
'adgroup'),
array(
'visits',
'visitBounceRate',
'pageviewsPerVisit',
'avgTimeOnSite',
'goal1Completions',
'transactionRevenue'));
?>
<table>
<tr>
<th>Date</th>
<th>Day</th>
<th>Campaign</th>
<th>Adgroup</th>
<th>Visits</th>
<th>Bounce Rate</th>
<th>Pages Per Visit</th>
<th>Avg Time On Site</th>
<th>Goal Completions</th>
<th>Revenue</th>
</tr>
<?php
foreach($ga->getResults() as $result):
?>
<tr>
<td><?php echo $result ?></td>
<td><?php echo date ('Y-n-j', strtotime($result->getDate()));?></td>
<td><?php echo $result->getdayOfWeek()?></td>
<td><?php echo $result->getCampaign()?></td>
<td><?php echo $result->getAdgroup()?></td>
<td><?php echo $result->getVisits()?></td>
<td><?php echo $result->getVisitBounceRate()?></td>
<td><?php echo $result->getpageviewsPerVisit()?></td>
<td><?php echo $result->getAvgTimeOnSite()?></td>
<td><?php echo $result->getgoal1Completions()?></td>
<td><?php echo $result->gettransactionRevenue()?></td>
</tr>-->
<?php
endforeach
?>
</table>
<?php
$fh = fopen('analytics.csv', 'w') or die ("Can't open analytics.csv");
foreach ($result as $yesterday) {
if (fputcsv($fh, $yesterday) == false) {
die("Can't write CSV line");
}
}
fclose($fh) or die ("Can't close analytics.csv");
echo "File 'analytics.csv' written successfully";
?>Thanks
Peter