So I am crawling my blog to detect which articles have issues (broken links and linking to products we no longer sell). So I am trying to get the traffic since the article was created to a year later (unless its been done within a year, in which case it is new), then add the traffic data for that. I am doing this to prioritise the list as there are over 1000 articles with issues.
So the function of getting the anlytics was taken from Google Analytics API documentation and modified:
function getResults($analytics, $profileId, $page, $startdays, $enddays) {
return $analytics->data_ga->get(
'ga:' . $profileId,
$startdays.'daysAgo',
$enddays.'daysAgo',
'ga:sessions',
array(
'filters' => 'ga:pagePath=='.$page.',ga:medium==organic'));
}
function printResults($results) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) > 0) {
// Get the profile name.
$profileName = $results->getProfileInfo()->getProfileName();
// Get the entry for the first entry in the first row.
$rows = $results->getRows();
$sessions = $rows[0][0];
// Print the results.
print "Total sessions: $sessions
";
} else {
print "No results found.
";
}
}
The code to actually put these parameters into the function is:
$sql="SELECT * FROM `articles_tbl` LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$url=$row["article_url"];
$published_date=$row["date_published"];
$your_date = strtotime($published_date);
$now = time();
$datediff=$now-$your_date;
$startdays = round($datediff / (60 * 60 * 24));
$enddays=$startdays-365;
if($enddays<0){
$enddays=1;
}
$analytics = initializeAnalytics();
$profile = getFirstProfileId($analytics);
//The domain has been removed for obvious reasons
//this is to get the folders in the URL:
//"/blog/news/pagefolder/"
$page=str_replace(DOMAIN,"",$url);
$results = getResults($analytics, $profile, $page, $startdays, $enddays);
printResults($results);
echo "for url: ".$page." between ".$startdays." days ago and ".$enddays." days ago.";
}
}
So this currently outputs: "Total sessions: 999999 for url: /blog/news/pagefolder/ between 859 days ago and 494 days ago.
Now this figure above is the whole website traffic for that period (confirmed in Analytics manually), but I want the exact number of sessions for that URL only. Nothing I do will show this or change the traffic in anyway. I can only assume it gives me the sessions before the filters are applied, thus it is always the same? I have been staring at this for hours and no luck, everything I have read says that by just doing pagePath== instead of pagePath=@ should make it exact, but no suck luck, still get all website traffic...