Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick Start Example #12

Closed
richzertuche opened this issue Jul 7, 2016 · 9 comments
Closed

Quick Start Example #12

richzertuche opened this issue Jul 7, 2016 · 9 comments

Comments

@richzertuche
Copy link

richzertuche commented Jul 7, 2016

Hi, following the quick-start example

$twitterAds = new TwitterAds(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), env('TWITTER_ACCESS_TOKEN'), env('TWITTER_ACCESS_TOKEN_SECRET'));

 $account = $twitterAds->getAccounts(env('ACCOUNT_ID'));

It always returns forbidden, the application is already accepted by twitter ads and the user already authorized the app.

TWITTER_CONSUMER_KEY =xxxxxxxxxxxxxxxx
TWITTER_CONSUMER_SECRET =xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWITTER_ACCESS_TOKEN =user-id-xxxxxxxxxxxxxxxxx
TWITTER_ACCESS_TOKEN_SECRET =xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The curl request actually retrieves the accounts, is it right that the TWITTER_ACCESS_TOKEN is made up by user-id-xxxxxxxxxxxx or should be only the xxxxxxxxxxxxx

Thanks.

@hborras
Copy link
Owner

hborras commented Jul 7, 2016

Hi @richzertuche ,

You should put exactly the content that Twitter returns to you in the authorization workflow.

Anyway, I think you're right, ACCESS_TOKEN is made of a combination of the user-id and some code you should put it all together.

Tell me if you have any issue with this.

@richzertuche
Copy link
Author

Thanks @hborras

Yeap figured it out just about a few hours ago :\

Right now I'm trying to get the analytics for all my CAMPAIGNs, no luck yet, everything returns empty, CAMPAIGNs do appear.

Thanks

@hborras
Copy link
Owner

hborras commented Jul 8, 2016

How are you making the requests?

@richzertuche
Copy link
Author

richzertuche commented Jul 8, 2016

$twitterAds = new TwitterAds(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), env('TWITTER_ACCESS_TOKEN'), env('TWITTER_ACCESS_TOKEN_SECRET'));
        $account = $twitterAds->getAccounts(env('ACCOUNT_ID'));
        $params = [
            'granularity' => TwitterAds\Enumerations::GRANULARITY_TOTAL
        ];
        $campaigns = $account->getCampaigns('', ['sort_by' => 'created_at-desc','count' => 5]);
        $metrics = [Analytics::ANALYTICS_METRIC_GROUPS_ENGAGEMENT, Analytics::ANALYTICS_METRIC_GROUPS_BILLING];

        $campaign = $campaigns->first();
        $stats = $campaign->stats($metrics, $params);

        $ids = array_map(
            function($o) {
                return $o->getId();
            },
            $campaigns->getCollection()
        );
        $stats2 = Campaign::all_stats($account, $ids, $metrics, $params);

I changed: class Campaign extends Analytics , to extend Analytics instead of Resource and const RESOURCE_STATS = 'stats/accounts/{account_id}/';, according to twitter doc.

@hborras
Copy link
Owner

hborras commented Jul 9, 2016

Well, I didn't implement that to campaigns due to the fact that twitter doesn't reccomend get stats at Campaign level.

I'll check your code and I'll say something to you!

@hborras
Copy link
Owner

hborras commented Jul 11, 2016

Hi @richzertuche,

I've tryed again analytics request and all is working perfect.

With line items, with your option -> ['sort_by' => 'created_at-desc','count' => 5] and making granularity TOTAL instead of hour I get this response:

`
Array
(
[0] => stdClass Object
(
[id] => 5itjm
[id_data] => Array
(
[0] => stdClass Object
(
[segment] =>
[metrics] => stdClass Object
(
[impressions] => Array
(
[0] => 297733
)

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 37800000
                                    )

                                [qualified_impressions] => 
                                [follows] => Array
                                    (
                                        [0] => 3
                                    )

                                [app_clicks] => 
                                [retweets] => Array
                                    (
                                        [0] => 2
                                    )

                                [likes] => Array
                                    (
                                        [0] => 17
                                    )

                                [engagements] => Array
                                    (
                                        [0] => 181
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 175
                                    )

                                [card_engagements] => 
                                [replies] => Array
                                    (
                                        [0] => 1
                                    )

                                [url_clicks] => Array
                                    (
                                        [0] => 68
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 57
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

[1] => stdClass Object
    (
        [id] => 5isf5
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => 
                                [billed_charge_local_micro] => 
                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => 
                                [clicks] => 
                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => 
                                [billed_engagements] => 
                                [carousel_swipes] => 
                            )

                    )

            )

    )

[2] => stdClass Object
    (
        [id] => 5hu8f
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 328
                                    )

                                [billed_charge_local_micro] => 
                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => 
                                [clicks] => 
                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => 
                                [billed_engagements] => 
                                [carousel_swipes] => 
                            )

                    )

            )

    )

[3] => stdClass Object
    (
        [id] => 5hu8i
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 2973
                                    )

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 1300000
                                    )

                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => Array
                                    (
                                        [0] => 4
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 4
                                    )

                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => Array
                                    (
                                        [0] => 2
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 1
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

[4] => stdClass Object
    (
        [id] => 5hu8g
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 2018
                                    )

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 600000
                                    )

                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => Array
                                    (
                                        [0] => 1
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 1
                                    )

                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => Array
                                    (
                                        [0] => 1
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 1
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

)
`

Doing the same with your code, what's a extension of mine. I get correct metrics and results:

`Array
(
[0] => stdClass Object
(
[id] => 5b275
[id_data] => Array
(
[0] => stdClass Object
(
[segment] =>
[metrics] => stdClass Object
(
[impressions] =>
[billed_charge_local_micro] =>
[qualified_impressions] =>
[follows] =>
[app_clicks] =>
[retweets] =>
[likes] =>
[engagements] =>
[clicks] =>
[card_engagements] =>
[replies] =>
[url_clicks] =>
[billed_engagements] =>
[carousel_swipes] =>
)

                    )

            )

    )

[1] => stdClass Object
    (
        [id] => 5b2kh
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 297733
                                    )

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 37800000
                                    )

                                [qualified_impressions] => 
                                [follows] => Array
                                    (
                                        [0] => 3
                                    )

                                [app_clicks] => 
                                [retweets] => Array
                                    (
                                        [0] => 2
                                    )

                                [likes] => Array
                                    (
                                        [0] => 17
                                    )

                                [engagements] => Array
                                    (
                                        [0] => 181
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 175
                                    )

                                [card_engagements] => 
                                [replies] => Array
                                    (
                                        [0] => 1
                                    )

                                [url_clicks] => Array
                                    (
                                        [0] => 68
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 57
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

[2] => stdClass Object
    (
        [id] => 55if9
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => 
                                [billed_charge_local_micro] => 
                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => 
                                [clicks] => 
                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => 
                                [billed_engagements] => 
                                [carousel_swipes] => 
                            )

                    )

            )

    )

[3] => stdClass Object
    (
        [id] => 5a80i
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 209056
                                    )

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 54270000
                                    )

                                [qualified_impressions] => 
                                [follows] => Array
                                    (
                                        [0] => 2
                                    )

                                [app_clicks] => 
                                [retweets] => 
                                [likes] => Array
                                    (
                                        [0] => 12
                                    )

                                [engagements] => Array
                                    (
                                        [0] => 208
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 201
                                    )

                                [card_engagements] => 
                                [replies] => Array
                                    (
                                        [0] => 5
                                    )

                                [url_clicks] => Array
                                    (
                                        [0] => 91
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 83
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

[4] => stdClass Object
    (
        [id] => 540sv
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => 
                                [billed_charge_local_micro] => 
                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => 
                                [clicks] => 
                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => 
                                [billed_engagements] => 
                                [carousel_swipes] => 
                            )

                    )

            )

    )

)
hborras@hector-ubuntu:~/projects/2016/php/twitter-php-ads-sdk/examples$ php analytics.php
Array
(
[0] => stdClass Object
(
[id] => 5b275
[id_data] => Array
(
[0] => stdClass Object
(
[segment] =>
[metrics] => stdClass Object
(
[impressions] =>
[billed_charge_local_micro] =>
[qualified_impressions] =>
[follows] =>
[app_clicks] =>
[retweets] =>
[likes] =>
[engagements] =>
[clicks] =>
[card_engagements] =>
[replies] =>
[url_clicks] =>
[billed_engagements] =>
[carousel_swipes] =>
)

                    )

            )

    )

[1] => stdClass Object
    (
        [id] => 5b2kh
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 297733
                                    )

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 37800000
                                    )

                                [qualified_impressions] => 
                                [follows] => Array
                                    (
                                        [0] => 3
                                    )

                                [app_clicks] => 
                                [retweets] => Array
                                    (
                                        [0] => 2
                                    )

                                [likes] => Array
                                    (
                                        [0] => 17
                                    )

                                [engagements] => Array
                                    (
                                        [0] => 181
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 175
                                    )

                                [card_engagements] => 
                                [replies] => Array
                                    (
                                        [0] => 1
                                    )

                                [url_clicks] => Array
                                    (
                                        [0] => 68
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 57
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

[2] => stdClass Object
    (
        [id] => 55if9
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => 
                                [billed_charge_local_micro] => 
                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => 
                                [clicks] => 
                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => 
                                [billed_engagements] => 
                                [carousel_swipes] => 
                            )

                    )

            )

    )

[3] => stdClass Object
    (
        [id] => 5a80i
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => Array
                                    (
                                        [0] => 209056
                                    )

                                [billed_charge_local_micro] => Array
                                    (
                                        [0] => 54270000
                                    )

                                [qualified_impressions] => 
                                [follows] => Array
                                    (
                                        [0] => 2
                                    )

                                [app_clicks] => 
                                [retweets] => 
                                [likes] => Array
                                    (
                                        [0] => 12
                                    )

                                [engagements] => Array
                                    (
                                        [0] => 208
                                    )

                                [clicks] => Array
                                    (
                                        [0] => 201
                                    )

                                [card_engagements] => 
                                [replies] => Array
                                    (
                                        [0] => 5
                                    )

                                [url_clicks] => Array
                                    (
                                        [0] => 91
                                    )

                                [billed_engagements] => Array
                                    (
                                        [0] => 83
                                    )

                                [carousel_swipes] => 
                            )

                    )

            )

    )

[4] => stdClass Object
    (
        [id] => 540sv
        [id_data] => Array
            (
                [0] => stdClass Object
                    (
                        [segment] => 
                        [metrics] => stdClass Object
                            (
                                [impressions] => 
                                [billed_charge_local_micro] => 
                                [qualified_impressions] => 
                                [follows] => 
                                [app_clicks] => 
                                [retweets] => 
                                [likes] => 
                                [engagements] => 
                                [clicks] => 
                                [card_engagements] => 
                                [replies] => 
                                [url_clicks] => 
                                [billed_engagements] => 
                                [carousel_swipes] => 
                            )

                    )

            )

    )

)
`

Could you check that campaigns are you feching are active?

@pgiani
Copy link

pgiani commented Jul 11, 2016

To get the stats for the Campaings add a new fuction on Analytics.php as fallow

`

public static function all_stats_campains(Account $account, $ids, $metricGroups, $params = [])
{

    $endTime = isset($params['end_time']) ? $params['end_time'] : new \DateTime('now');
    $endTime->setTime($endTime->format('H'), 0, 0);
    $startTime = isset($params['start_time']) ? $params['start_time'] : new \DateTime($endTime->format('c')." - 7 days");
    $startTime->setTime($startTime->format('H'), 0, 0);
    $granularity = isset($params['granularity']) ? $params['granularity'] : Enumerations::GRANULARITY_HOUR;
    $placement = isset($params['placement']) ? $params['placement'] : Enumerations::PLACEMENT_ALL_ON_TWITTER;
    $segmentationType = isset($params['segmentation_type']) ? $params['segmentation_type'] : null;

    $params = [
         'metric_groups' => implode(",", $metricGroups),
        'start_time' => $startTime->format('c'),
        'end_time' => $endTime->format('c'),
        'granularity' => $granularity,
        'entity' => 'CAMPAIGN',
        'entity_ids' => implode(",", $ids),
        'placement' => $placement,
    ];

    if (!is_null($segmentationType)) {
        $params['segmentation_type'] = $segmentationType;
    }


    $resource = str_replace(static::RESOURCE_REPLACE, $account->getId(), static::RESOURCE_STATS);
    $request = $account->getTwitterAds()->get($resource, $params);

    return $request->data;
}`

I just call all_stats_LineItem::campains insted of LineItem::all_stats andd it works fine, I did not see any warnings againts calling campaigns analytics on the Documentation on Twiiter API for version 1, but I could be wrong

@hborras
Copy link
Owner

hborras commented Jul 12, 2016

In this case, it's even easier, you only need to extend Campaign class to Analytics instead of Resource.

That's what I meant when I say about Analytics requests and Best practices:

https://dev.twitter.com/ads/analytics/best-practices

Do request data (ideally) at the line_items and promoted_tweets level, as you can always aggregate and roll these metrics up to get totals across the entire ads entity hierarchy (i.e. for the campaign, funding instrument or account levels).

But, since It could be a desired request, I'll implement it

@hborras
Copy link
Owner

hborras commented Jul 20, 2016

Closed since not response from @richzertuche

@hborras hborras closed this as completed Jul 20, 2016
@ghost ghost mentioned this issue Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants