Twitter APIs for #MediaHackday

26
Twitter APIs Andy Piper Developer Advocate @andypiper

Transcript of Twitter APIs for #MediaHackday

Twitter APIs

Andy Piper Developer Advocate

@andypiper

Twitter APIs deal both with the moment and the past

The Moment: Streaming API t.co/streaming 1% sample, or track keywords

The Past: REST API t.co/rest Optimize your calls

4

Create your app: easy t.co/appsDon’t re-invent the wheel: libs t.co/libs

@andypiper

Help?Join the conversation in the Twitter Community forums

Follow @TwitterAPI, @AdsAPI and @TwitterDev for platform updates

Developer ResourcesDocumentation dev.twitter.com

Streaming API t.co/streaming

REST API t.co/rest

Create App t.co/apps

Open Source Libs t.co/libs

Code Examples t.co/code

Crashlytics Answers Twitter Digits MoPub

A D S A P I

t.co/adsapi

G E T T I N G S T A R T E D

</>PRE-REQUISITES

SETUP

RUBY VERSION

$ ruby -vruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

$ gem install pry jsonpretty twurl

INSTALLING TOOLS & DEPENDENCIES

Note: Depending on how your Ruby installation is setup, you may needto run the above “gem install” commands with “sudo”.

AU T H O R I Z AT I O N

More info at: https://dev.twitter.com/oauth

OAU T H 1 . 0 A

The Ads API uses OAuth 1.0a for authorization and implements the 3-legged OAuth flow.

AU T H O R I Z AT I O N API key and secret are available under the “Keys and Access Tokens” tab.

CO N S U M E R K E Y & S E C R E T

Your consumer key is a publicly visible identifier for your app.

You should never share your consumer secret.

</>PRE-REQUISITES

SETUP

AU T H O R I Z AT I O N

$ twurl authorize --consumer-key key --consumer-secret secret

VA L I DAT E S E T U P

$ cat ~/.twurlrc

TRY IT OUT

$ twurl -H ads-api-sandbox.twitter.com "/1/accounts" | jsonpretty

PER MINS / ENDPOINT

25 REQUESTS

READ S

PER MIN / C ATEGORY

100 REQUESTS

WRITES

D E V E L O P E R R AT E L I M I T S

More info at: https://dev.twitter.com/ads/basics/rate-limiting

W O R K S H O P

</>INSTALLING THE RUBY SDK

$ gem install twitter-ads

RUBY SDK

SETUP

$ twitter-adstwitter-ads v0.3.4 >>

STA R T A N I N T E R AC T I V E S E S S I O N

Note: Depending on how your Ruby installation is setup, you may needto run the above “gem install” commands with “sudo”.

</>RUBY SDK

CLIENT

# enable sandbox mode CLIENT.options[:sandbox] = true

# load up the account instance account = CLIENT.accounts.first

# enable request tracing (optional,= true

good for troubleshooting)CLIENT.options[:trace]

http://bit.ly/ads-api-client

</>http://bit.ly/ads-api-campaign

RUBY SDK

CAM PAIGN

# createcampaign = TwitterAds::Campaign.new(account)

your campaign

campaign.funding_instrument_id = account.funding_instruments.first.idcampaign.daily_budget_amount_local_micro = 1_000_000campaign.name = 'my first campaign'campaign.paused = truecampaign.start_time = Time.now.utccampaign.save

</>http://bit.ly/ads-api-line-item

RUBY SDK

LINE ITEM

# create a line item for the campaignline_item = TwitterAds::LineItem.new(account)

line_item.name = 'my first ad'line_item.product_type = TwitterAds::Product::PROMOTED_TWEETSline_item.placements = [TwitterAds::Placement::ALL_ON_TWITTER]line_item.objective = TwitterAds::Objective::TWEET_ENGAGEMENTSline_item.bid_amount_local_micro = 10_000line_item.paused = trueline_item.save

line_item.campaign_id = campaign.id

TWEET RETWEET PROMOTED TWEET

</>http://bit.ly/ads-api-promoted-tweet

RUBY SDK

PROMOTED TWEET

# create resource

request for a simple null-casted tweet= "/1/accounts/#{account.id}/tweet"

tweet_params = { status: ‘Hello @AdsAPI!’ }request = TwitterAds::Request.new(CLIENT, :post, resource, params: tweet_params)tweet = request.perform

# promote the tweet using our line item

promoted_tweet = TwitterAds::Creative::PromotedTweet.new(account)

promoted_tweet.line_item_id = line_item.id

promoted_tweet.tweet_id = tweet.body[:data][:id]

promoted_tweet.save

</>http://bit.ly/ads-api-targeting

RUBY SDK

TA R G E T I N G

# fetching targeting criteria values

resource = '/1/targeting_criteria/locations'

params = { location_type: 'COUNTRY', q: 'u' }

request = TwitterAds::Request.new(CLIENT, :get, resource, params: params)

cursor = TwitterAds::Cursor.new(nil, request)

# add targeting criteria

targeting_criteria = TwitterAds::TargetingCriteria.new(account)

targeting_criteria.line_item_id = line_item.id

targeting_criteria.targeting_type = 'LOCATION'

targeting_criteria.targeting_value = '00a8b25e420adc94'

targeting_criteria.save

</>http://bit.ly/ads-api-analytics

RUBY SDK

ANA LYTICS

# limit request count and grab the first 10 line items from TwitterAds::Cursor

line_items = account.line_items(nil, count: 10)[0..9]

# the list of metrics we want to fetch

metrics = [:billed_engagements, :billed_follows]

# fetching stats on the instance

line_items.first.stats(metrics)

# fetching stats for multiple line items

ids = line_items.map { |line_item| line_item.id }

TwitterAds::LineItem.stats(account, ids, metrics)

#THANKYOU