10 defined(
'JPATH_PLATFORM') or die;
31 parent::__construct($options, $auth);
33 if (isset($this->auth) && !$this->auth->getOption(
'scope'))
35 $this->auth->setOption(
'scope',
'https://www.googleapis.com/auth/adsense');
49 public function getAccount($accountID, $subaccounts =
true)
51 if ($this->isAuthenticated())
53 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) . ($subaccounts ?
'?tree=true' :
'');
54 $jdata = $this->query($url);
56 if ($data = json_decode($jdata->body,
true))
62 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
82 public function listAccounts($options = array(), $maxpages = 1)
84 if ($this->isAuthenticated())
86 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
87 unset($options[
'nextPageToken']);
88 $url =
'https://www.googleapis.com/adsense/v1.1/accounts?' . http_build_query($options);
90 return $this->listGetData($url, $maxpages, $next);
110 public function listClients($accountID, $options = array(), $maxpages = 1)
112 if ($this->isAuthenticated())
114 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
115 unset($options[
'nextPageToken']);
116 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) .
'/adclients?' . http_build_query($options);
118 return $this->listGetData($url, $maxpages, $next);
137 public function getUnit($accountID, $adclientID, $adunitID)
139 if ($this->isAuthenticated())
141 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID);
142 $url .=
'/adclients/' . urlencode($adclientID) .
'/adunits/' . urlencode($adunitID);
143 $jdata = $this->query($url);
145 if ($data = json_decode($jdata->body,
true))
151 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
174 public function listUnitChannels($accountID, $adclientID, $adunitID, $options = array(), $maxpages = 1)
176 if ($this->isAuthenticated())
178 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
179 unset($options[
'nextPageToken']);
180 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID);
181 $url .=
'/adclients/' . urlencode($adclientID) .
'/adunits/' . urlencode($adunitID) .
'/customchannels?' . http_build_query($options);
183 return $this->listGetData($url, $maxpages, $next);
202 public function getChannel($accountID, $adclientID, $channelID)
204 if ($this->isAuthenticated())
206 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) .
'/adclients/';
207 $url .= urlencode($adclientID) .
'/customchannels/' . urlencode($channelID);
208 $jdata = $this->query($url);
210 if ($data = json_decode($jdata->body,
true))
216 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
238 public function listChannels($accountID, $adclientID, $options = array(), $maxpages = 1)
240 if ($this->isAuthenticated())
242 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
243 unset($options[
'nextPageToken']);
244 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) .
'/adclients/' . urlencode($adclientID);
245 $url .=
'/customchannels?' . http_build_query($options);
247 return $this->listGetData($url, $maxpages, $next);
269 public function listChannelUnits($accountID, $adclientID, $channelID, $options = array(), $maxpages = 1)
271 if ($this->isAuthenticated())
273 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
274 unset($options[
'nextPageToken']);
275 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) .
'/adclients/' . urlencode($adclientID);
276 $url .=
'/customchannels/' . urlencode($channelID) .
'/adunits?' . http_build_query($options);
278 return $this->listGetData($url, $maxpages, $next);
299 public function listUrlChannels($accountID, $adclientID, $options = array(), $maxpages = 1)
301 if ($this->isAuthenticated())
303 $next = array_key_exists(
'nextPageToken', $options) ? $options[
'nextPage'] : null;
304 unset($options[
'nextPageToken']);
305 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID);
306 $url .=
'/adclients/' . urlencode($adclientID) .
'/urlchannels?' . http_build_query($options);
308 return $this->listGetData($url, $maxpages, $next);
330 public function generateReport($accountID, $start, $end =
false, $options = array(), $maxpages = 1)
332 if ($this->isAuthenticated())
336 $startobj =
new DateTime;
337 $startobj->setTimestamp($start);
339 elseif (is_string($start))
341 $startobj =
new DateTime($start);
343 elseif (is_a($start,
'DateTime'))
349 throw new InvalidArgumentException(
'Invalid start time.');
354 $endobj =
new DateTime;
356 elseif (is_int($end))
358 $endobj =
new DateTime;
359 $endobj->setTimestamp($end);
361 elseif (is_string($end))
363 $endobj =
new DateTime($end);
365 elseif (is_a($end,
'DateTime'))
371 throw new InvalidArgumentException(
'Invalid end time.');
374 $options[
'startDate'] = $startobj->format(
'Y-m-d');
375 $options[
'endDate'] = $endobj->format(
'Y-m-d');
377 unset($options[
'startIndex']);
379 $url =
'https://www.googleapis.com/adsense/v1.1/accounts/' . urlencode($accountID) .
'/reports?' . http_build_query($options);
381 if (strpos($url,
'&'))
387 $data[
'rows'] = array();
391 $jdata = $this->query($url .
'startIndex=' . count($data[
'rows']));
392 $newdata = json_decode($jdata->body,
true);
394 if ($newdata && array_key_exists(
'rows', $newdata))
396 $newdata[
'rows'] = array_merge($data[
'rows'], $newdata[
'rows']);
401 throw new UnexpectedValueException(
"Unexpected data received from Google: `{$jdata->body}`.");
406 while (count($data[
'rows']) < $data[
'totalMatchedRows'] && $i < $maxpages);