Closed ghost closed 4 years ago
Hello @eblucas-uk
You have this line "$lineItems->setUseImplicitFetch(true);" It means that automatically Cursor class will fetch another request. If you don't set it true, (aka remove that line) you will need manually at the end of the loop call "$lineItems->fetchNext()"
Hello
Thanks for your reply When I use $lineItems->fetchNext();
I get bad request error
Hborras \ TwitterAdsSDK \ TwitterAds \ Errors \ BadRequest (400) BAD_REQUEST
Could you share the whole code and where are you using the "fetchNext()" instruction?
Thanks
public function getTwitterData(Request $request) { $agent = new Agent(['sourceRoot' => realpath('/app')]); $this->logging = new logging('TwitterApi'); $this->logging->getLogger()->info('Started Twitter'); $this->content = Request::all();
// $this->account_id = $this->content['account_id'];
// $this->account_id = "xxxxxxxx";
$this->account_id = "xxxxx";
$api = TwitterAds::init($this->CONSUMER_KEY, $this->CONSUMER_SECRET, $this->ACCESS_TOKEN, $this->ACCESS_TOKEN_SECRET);
$account = new Account($this->account_id);
$account->read();
$campaigns = $account->getCampaigns();
// $campaigns->setUseImplicitFetch(true);
$campaignsData = [];
$TwitterData = array();
$i = 0;
$i1 = 0;
$this->logging->getLogger()->info('Getting Campaigns');
foreach ($campaigns as $campaign) {
$i1++;
// $campaignDate = $campaign->getStartTime()->format('Y-m-d');
// $campaignname = $campaign->getName();
$d = date('Y-m-d', strtotime('today -30days'));
$endDate = ($campaign->getEndTime()->format('Y-m-d'));
if($endDate > $d){
$i++;
$campaignsData[] = $campaign;
}
}
foreach ($campaignsData as $campaign) {
$lineItems = $campaign->getLineItems();
// $lineItems->setUseImplicitFetch(true);
foreach ($lineItems as $lineItem) {
// echo "<pre>";
// print_r($lineItem);
// echo "</pre>";
try {
$async = true;
$startDate = new \DateTime($campaign->getStartTime()->format('Y-m-d 00:00:00'), new \DateTimeZone('Europe/London'));
$endDate = new \DateTime('now 00:00:00',new \DateTimeZone('Europe/London'));
$dates = $this->dateRanges($startDate, $endDate);
foreach ($dates as $date) {
// echo "<pre>";
// print_r($date);
// echo "</pre>";
$job = $lineItem->stats(
[
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_BILLING,
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_ENGAGEMENT,
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_MEDIA,
TwitterAds\Fields\AnalyticsFields::METRIC_GROUPS_VIDEO,
],
[
TwitterAds\Fields\AnalyticsFields::START_TIME => $date[0],
// TwitterAds\Fields\AnalyticsFields::END_TIME => $date[1],
AnalyticsFields::END_TIME => $date[1],
AnalyticsFields::GRANULARITY => Enumerations::GRANULARITY_DAY,
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;
$this->logging->getLogger()->info('Job is still processing. Waiting 5 more seconds');
$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)) {
$spendOrg = $this->getStats($stats->billed_charge_local_micro);
$spend = $spendOrg/1000000;
$TwitterData[] = [
'Campaign' => $campaign->getName(),
'CampaignId' => $campaign->getId(),
'AdGroup' => $lineItem->getName(),
'Date' => $date[0],
'CampaignStart' => $campaign->getStartTime()->format('Y-m-d'),
'CampaignEnd' => $campaign->getEndTime()->format('Y-m-d'),
'Objective' => $lineItem->getObjective(),
'Impressions' => $this->getStats($stats->impressions),
'Spend' => str_replace(',','', number_format($spend, 2)),
'ASBOF'=> number_format($spend * 0.001, 5),
'VideoViews' => $this->getStats($stats->video_total_views),
'VideoPlayed25' => $this->getStats($stats->video_views_25),
'VideoPlayed50' => $this->getStats($stats->video_views_50),
'VideoPlayed75' => $this->getStats($stats->video_views_75),
'VideoCompletions' => $this->getStats($stats->video_views_75),
'Engagements' => $this->getStats($stats->engagements),
'Retweets' => $this->getStats($stats->retweets),
'Replies' => $this->getStats($stats->replies),
'Likes' => $this->getStats($stats->likes),
'ThreeS100SVideoViews' => $this->getStats($stats->video_3s100pct_views),
'LinkClicks' => $this->getStats($stats->url_clicks),
'Clicks' => $this->getStats($stats->clicks),
];
}
}
}
}catch (\Hborras\TwitterAdsSDK\TwitterAdsException $e) {
print_r($e->getErrors());
}
}
// $lineItems->fetchNext();
}
// exit();
echo "<pre>";
print_r($TwitterData);
echo "</pre>";
// $this->logging->getLogger()->info('Returned Data');
// return response()->json($TwitterData);
}
Even if I dont use $lineItems->setUseImplicitFetch(true); it never stops running for some account ids... Cant understand why.. there is only 4 camapings with 5 line itens in each campaign but never finishes, its been runing over an our now. For other account id with more campaigns and more line itens it dont happen.
Thanks for your help on this, if you have a better way of doing this I appreciate.
I need to get the data for all campaigns in each accountId, thats what I trying to do
For those accountes where it runs forever. Could it be, because they where created much more time ago?
Take a look at this line:
$startDate = new \DateTime($campaign->getStartTime()->format('Y-m-d 00:00:00'), new \DateTimeZone('Europe/London'));
Hmm I haven't thought about that.
Make sense, will change and let you know. Thank you
Hi Hborras
I'm trying to get only 1 campaign but it seems that the count isnt working
$campaigns = $account->getCampaigns('', [ 'sort_by' => 'created_at-asc','count' => 1]);
it is returning all campaigns. The sort by is working though.
Do you know how I can get only one?
have you tried
$campaign = new Campaign($campaignId);
$campaign->read();
Do you know why I get this error sometimes? I'm getting data from 4 campaigns but for only one I get this error
ErrorException thrown with message "Undefined index: retry-after"
Stacktrace:
Hi Hborras,
Has anything changed?
Now I get this error when I try to access the accounts.
Hborras \ TwitterAdsSDK \ TwitterAds \ Errors \ Forbidden (403) FORBIDDEN
Yesterday it was working normally
Have you updated to v6?
Yesterday Twitter deleted v5 access, you need to update the library to v6
Ok thanks.. didnt know that.. its working now
Do you know why I get this error sometimes? I'm getting data from 4 campaigns after a few minutes running I get this error.
ErrorException thrown with message "Undefined index: retry-after"
Stacktrace:
Let me check it!
Thanks!
Twitter Ads API is throwing a 503 code. That code should include a header called "retry-after". Looks like Twitter is not always showing that header.
Download latest version of the library. You will get an error anyway. But the reason is that you're hitting same endpoint too heavily
I downloaded this morning.
Thanks for the feedback. I'll have to split the requests then.
Hi Borras,
I'm trying to get ads data from my campaigns, for some it works fine for others it never finishes running.
How could I get less data?
This is what I'm doing but doesn't seem to work. Just never stop running and when I go to the UI I see only a few line itens for this add count.
private function dateRanges($startTime, $endTime) { $interval = new \DateInterval('P1D'); $dateRange = new \DatePeriod($startTime, $interval, $endTime);
}
Could you please help me to find a better way using your package?