Skip to content

Commit

Permalink
Added new exampled. Added new code to get TailoredAudiences
Browse files Browse the repository at this point in the history
  • Loading branch information
hborras committed Nov 25, 2017
1 parent 5c5f747 commit 7209c80
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 10 deletions.
5 changes: 3 additions & 2 deletions examples/analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
$l = 1;
foreach ($campaignsData as $campaign) {
echo $i . ": " . $campaign->getId() . " " . $campaign->getName() . " " . $campaign->getStartTime()->format('Y-m-d') . " - " . $campaign->getEntityStatus() . PHP_EOL;
$lineItems = $campaign->getLineItems();
$lineItems = $campaign->getLineItems([TwitterAds\Fields\LineItemFields::COUNT => 2]);
/** @var LineItem $lineItem */
foreach ($lineItems as $lineItem) {
echo "\t" . $j . ": " . $lineItem->getId() . " " . $lineItem->getName() . " " . PHP_EOL;
Expand All @@ -59,6 +59,7 @@
}
$l = 1;
try {
$async = false;
$startDate = new \DateTime($campaign->getStartTime()->format('Y-m-d 00:00:00'));
$endDate = new \DateTime('now');
$dates = dateRanges($startDate, $endDate);
Expand All @@ -74,7 +75,7 @@
AnalyticsFields::END_TIME => $date[1],
AnalyticsFields::GRANULARITY => Enumerations::GRANULARITY_TOTAL,
AnalyticsFields::PLACEMENT => Enumerations::PLACEMENT_ALL_ON_TWITTER
], true
], $async
);
$stats = $stats[0]->id_data[0]->metrics;
if (!is_null($stats->billed_charge_local_micro)) {
Expand Down
160 changes: 160 additions & 0 deletions examples/analytics_async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

use Hborras\TwitterAdsSDK\TwitterAds;
use Hborras\TwitterAdsSDK\TwitterAds\Account;
use Hborras\TwitterAdsSDK\TwitterAds\Analytics;
use Hborras\TwitterAdsSDK\TwitterAds\Campaign\Campaign;
use Hborras\TwitterAdsSDK\TwitterAds\Campaign\LineItem;
use Hborras\TwitterAdsSDK\TwitterAds\Enumerations;
use Hborras\TwitterAdsSDK\TwitterAds\Fields\AnalyticsFields;

require '../autoload.php';

const CONSUMER_KEY = 'your consumer key';
const CONSUMER_SECRET = 'your consumer secret';
const ACCESS_TOKEN = 'your access token';
const ACCESS_TOKEN_SECRET = 'your access token secret';
const ACCOUNT_ID = 'account id';

// Create Twitter Ads Api Instance
$api = TwitterAds::init(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

$accounts = $api->getAccounts();
// load up the account instance, campaign and line item
$account = new Account(ACCOUNT_ID);
$account->read();
$campaigns = $account->getCampaigns('', [TwitterAds\Fields\CampaignFields::COUNT => 1]);
$campaigns->setUseImplicitFetch(false);
$campaignsData = [];


/** @var Campaign $campaign */
foreach ($campaigns as $campaign) {
$campaignsData[] = $campaign;
}

$i = 1;
$j = 1;
$l = 1;
foreach ($campaignsData as $campaign) {
echo $i . ": " . $campaign->getId() . " " . $campaign->getName() . " " . $campaign->getStartTime()->format('Y-m-d') . " - " . $campaign->getEntityStatus() . PHP_EOL;
$lineItems = $campaign->getLineItems([TwitterAds\Fields\LineItemFields::COUNT => 2]);
/** @var LineItem $lineItem */
foreach ($lineItems as $lineItem) {
echo "\t" . $j . ": " . $lineItem->getId() . " " . $lineItem->getName() . " " . PHP_EOL;
echo "\t\tBid: " . ($lineItem->getBidAmountLocalMicro() / 1000000) . PHP_EOL;
echo "\t\tObjective: " . $lineItem->getObjective() . PHP_EOL;
echo "\t\tCharge By: " . $lineItem->getChargeBy() . PHP_EOL;
echo "\t\tBid Unit: " . $lineItem->getBidUnit() . PHP_EOL;
echo "\t\tOptimization: " . $lineItem->getOptimization() . PHP_EOL;
echo "\t\tBid Type: " . $lineItem->getBidType() . PHP_EOL;
$targetingCriterias = $lineItem->getTargetingCriteria();
/** @var TwitterAds\Campaign\TargetingCriteria $targetingCriteria */
foreach ($targetingCriterias as $targetingCriteria) {
echo "\t\t" . $l . ": " . $targetingCriteria->getId() . " " . $targetingCriteria->getName() . " " .

$targetingCriteria->getTargetingType() . " " . $targetingCriteria->getTargetingValue() . PHP_EOL;

$l++;
}
$l = 1;
try {
$async = true;
$startDate = new \DateTime($campaign->getStartTime()->format('Y-m-d 00:00:00'));
$endDate = new \DateTime('now');
$dates = dateRanges($startDate, $endDate);
foreach ($dates as $date) {
$job = $lineItem->stats(
[
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_BILLING,
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_MOBILE_CONVERSION,
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_ENGAGEMENT,
],
[
TwitterAds\Fields\AnalyticsFields::START_TIME => $date[0],
AnalyticsFields::END_TIME => $date[1],
AnalyticsFields::GRANULARITY => Enumerations::GRANULARITY_TOTAL,
AnalyticsFields::PLACEMENT => Enumerations::PLACEMENT_ALL_ON_TWITTER
], $async
);
while($job->getStatus() == TwitterAds\Fields\JobFields::PROCESSING){
echo "Job is still processing. Waiting 5 more seconds".PHP_EOL;
$job->read();
sleep(5);
}

if($job->getStatus() == TwitterAds\Fields\JobFields::SUCCESS){
$result = gzfile($job->getUrl());
$result = implode("", $result);
$stats = json_decode($result)->data;
$stats = $stats[0]->id_data[0]->metrics;
if (!is_null($stats->billed_charge_local_micro)) {
echo "\t\t\t Start: " . $date[0]->format('Y-m-d H:i:s') . PHP_EOL;
echo "\t\t\t End: " . $date[1]->format('Y-m-d H:i:s') . PHP_EOL;
echo "\t\t\t " . ($stats->billed_charge_local_micro[0] / 1000000) . "" . PHP_EOL;
echo "\t\t\t\t App clicks: ";
getStats($stats->app_clicks);
echo "\t\t\t\t Installs:" . PHP_EOL;
getStats($stats->mobile_conversion_installs);
echo "\t\t\t\t Checkouts:" . PHP_EOL;
getStats($stats->mobile_conversion_checkouts_initiated);
}
}
}

} catch (\Hborras\TwitterAdsSDK\TwitterAdsException $e) {
print_r($e->getErrors());
}

$j++;
}
$j = 1;
$i++;
}

function getStats($stat)
{
if ($stat instanceof stdClass) {
foreach (get_object_vars($stat) as $key => $val) {
if (is_array($val)) {
echo "\t\t\t\t\t " . $key . ": " . $val[0] . PHP_EOL;
} else {
echo "\t\t\t\t\t " . $key . " 0" . PHP_EOL;
}
}
} else if (is_array($stat)) {
foreach ($stat as $s) {
echo $s . PHP_EOL;
}
}

}

/**
* @param $startTime
* @param $endTime
* @return array
*/
function dateRanges($startTime, $endTime)
{
$interval = new \DateInterval('P60D');
$dateRange = new \DatePeriod($startTime, $interval, $endTime);

$previous = null;
$dates = array();
foreach ($dateRange as $dt) {
$current = $dt;
if (!empty($previous)) {
$show = $current;
$dates[] = array($previous, $show);
}
$previous = $current;
}
if (isset($dates[count($dates) - 1])) {
$dates[] = array($dates[count($dates) - 1][1], $endTime);
} else {
$dates[] = array($startTime, $endTime);
}

return $dates;
}
30 changes: 30 additions & 0 deletions examples/tailored_audience_read.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Hborras\TwitterAdsSDK\TwitterAds;
use Hborras\TwitterAdsSDK\TwitterAds\Account;
use Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience\TailoredAudience;


require '../autoload.php';

const CONSUMER_KEY = 'your consumer key';
const CONSUMER_SECRET = 'your consumer secret';
const ACCESS_TOKEN = 'your access token';
const ACCESS_TOKEN_SECRET = 'your access token secret';
const ACCOUNT_ID = 'account id';

/** Create twitter ads client */
$api = TwitterAds::init(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);


// Retrieve account information
$account = new Account(ACCOUNT_ID);
$account->read();

$tailoredAudiences = $account->getTailoredAudiences();

$tailoredAudiences->setDefaultUseImplicitFetch(true);

foreach ($tailoredAudiences as $tailoredAudience) {
echo $tailoredAudience->getName().PHP_EOL;
}
File renamed without changes.
11 changes: 9 additions & 2 deletions src/TwitterAds/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Hborras\TwitterAdsSDK\TwitterAds\Campaign\PromotableUser;
use Hborras\TwitterAdsSDK\TwitterAds\Creative\Video;
use Hborras\TwitterAdsSDK\TwitterAds\Fields\AnalyticsFields;
use Hborras\TwitterAdsSDK\TwitterAds\TailoredAudience\TailoredAudience;
use Hborras\TwitterAdsSDK\TwitterAdsException;
use Hborras\TwitterAdsSDK\TwitterAds\Fields\AccountFields;

Expand Down Expand Up @@ -172,9 +173,15 @@ public function getJobs($params = [])
}


public function getTailoredAudiences($id = '', $params = [])
/**
* @param array $params
* @return Cursor|Resource
*/
public function getTailoredAudiences($params = [])
{
// TODO: Next Release
$tailoredAudienceClass = new TailoredAudience();

return $tailoredAudienceClass->loadResource('', $params);
}

/**
Expand Down
24 changes: 23 additions & 1 deletion src/TwitterAds/Analytics/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
namespace Hborras\TwitterAdsSDK\TwitterAds\Analytics;

use Hborras\TwitterAdsSDK\TwitterAds\Analytics;
use Hborras\TwitterAdsSDK\TwitterAds\Fields\JobFields;

class Job extends Analytics
{
const RESOURCE_COLLECTION = 'stats/jobs/accounts/{account_id}';
const RESOURCE = 'stats/jobs/accounts/{account_id}/{id}';
const RESOURCE = 'stats/jobs/accounts/{account_id}';
const ENTITY = 'JOBS';

/** Read Only */
Expand All @@ -35,6 +36,27 @@ class Job extends Analytics

protected $properties = [];


public function read($params = [])
{
$resource = str_replace(static::RESOURCE_REPLACE, $this->getTwitterAds()->getAccountId(), static::RESOURCE);
$params[JobFields::JOB_IDS] = $this->id;
$response = $this->getTwitterAds()->get($resource, $params);
if(isset($response->getBody()->data[0])){
return $this->fromResponse($response->getBody()->data[0]);
} else {
return $this;
}
}

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @return mixed
*/
Expand Down
3 changes: 2 additions & 1 deletion src/TwitterAds/Campaign/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Hborras\TwitterAdsSDK\TwitterAds\Campaign;

use Hborras\TwitterAdsSDK\TwitterAds\Analytics;
use Hborras\TwitterAdsSDK\TwitterAds\Analytics\Job;
use Hborras\TwitterAdsSDK\TwitterAds\Creative\PromotedTweet;
use Hborras\TwitterAdsSDK\TwitterAds\Cursor;
use Hborras\TwitterAdsSDK\TwitterAds\Fields\AnalyticsFields;
Expand Down Expand Up @@ -82,7 +83,7 @@ public function getPromotedTweets($params = [])
* @param $metricGroups
* @param array $params
* @param bool $async
* @return mixed
* @return Job| mixed
*/
public function stats($metricGroups, $params = [], $async = false)
{
Expand Down
2 changes: 1 addition & 1 deletion src/TwitterAds/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function fetched()
*/
public function next()
{
if ($this->current_index == $this->getIndexRight()) {
if ($this->current_index == $this->getIndexRight() && !is_null($this->next_cursor)) {
if ($this->getUseImplicitFetch()) {
$this->fetchNext();
if ($this->current_index == $this->getIndexRight()) {
Expand Down
3 changes: 3 additions & 0 deletions src/TwitterAds/Fields/JobFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ class JobFields
const UPDATED_AT = 'updated_at';
const METRIC_GROUPS = 'metric_groups';
const JOB_IDS = 'job_ids';
const PROCESSING = 'PROCESSING';
const SUCCESS = 'SUCCESS';
const CANCELLED = 'CANCELLED';
}
5 changes: 5 additions & 0 deletions src/TwitterAds/Fields/LineItemFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ class LineItemFields
const ADVERTISER_DOMAIN = 'advertiser_domain';
const ADVERTISER_USER_ID = 'advertiser_user_id';
const LINE_ITEM_IDS = 'line_item_ids';
const WITH_DELETED = 'with_deleted';
const COUNT = 'count';
const SORT_BY = 'sort_by';
const DRAFT_ONLY = 'draft_only';
const ENTITY_STATUS = 'entity_status';
}
6 changes: 3 additions & 3 deletions tests/TwitterAds/TailoredAudience/TailoredAudienceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public function testTailoredAudiencesWillThrowAnExceptionWithAnInvalidType()

public function testTailoredAudiencesCanBeAddedFetchedAndDeletedSuccessfully()
{
$audience = new TailoredAudience($this->account);
$audience = new TailoredAudience();
$audience->setListType(TailoredAudience::LIST_TYPE_EMAIL);
$audience->setName('test audience');
$newAudience = $audience->save();

$fetched = (new TailoredAudience($this->account))->load($newAudience->getId());
$fetched = (new TailoredAudience())->load($newAudience->getId());
$this->assertEquals($fetched->getId(), $newAudience->getId());
$this->assertEquals($fetched->getName(), $newAudience->getName());
$this->assertEquals($fetched->getListType(), $newAudience->getListType());
Expand All @@ -35,7 +35,7 @@ public function testTailoredAudiencesCanBeAddedFetchedAndDeletedSuccessfully()

public function testTailoredAudiencesCanBeFetched()
{
$audience = new TailoredAudience($this->account);
$audience = new TailoredAudience();
$result = iterator_to_array($audience->all());

$this->assertGreaterThan(0, $result);
Expand Down

0 comments on commit 7209c80

Please sign in to comment.