Skip to content

Instantly share code, notes, and snippets.

@brndnblck
Last active April 9, 2016 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brndnblck/492b059893953c16d77a to your computer and use it in GitHub Desktop.
Save brndnblck/492b059893953c16d77a to your computer and use it in GitHub Desktop.
Twitter Ads API Getting Started (Ruby)
# enable "sandbox" mode
CLIENT.options[:sandbox] = true
# load your sandbox account object
account = CLIENT.accounts.first
# create your campaign
campaign = TwitterAds::Campaign.new(account)
campaign.funding_instrument_id = account.funding_instruments.first.id
campaign.daily_budget_amount_local_micro = 1_000_000
campaign.name = 'my first campaign'
campaign.paused = true
campaign.start_time = Time.now.utc
campaign.save
# create a line item for the campaign
line_item = TwitterAds::LineItem.new(account)
line_item.campaign_id = campaign.id
line_item.name = 'my first ad'
line_item.product_type = TwitterAds::Product::PROMOTED_TWEETS
line_item.placements = [TwitterAds::Placement::ALL_ON_TWITTER]
line_item.objective = TwitterAds::Objective::TWEET_ENGAGEMENTS
line_item.bid_amount_local_micro = 10_000
line_item.paused = true
line_item.save
# create request for a simple null-casted tweet
resource = "/0/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
# create a simple app download card (see https://dev.twitter.com/ads/creative)
app_card = TwitterAds::Creative::AppDownloadCard.new(account)
app_card.name = 'my first card'
app_card.app_country_code = 'US'
app_card.iphone_app_id = 333903271
app_card.app_cta = 'INSTALL_OPEN'
app_card.save
# create request for a null-casted tweet with an app download card
resource = "/0/accounts/#{account.id}/tweet"
tweet_params = { status: "Hello @AdsAPI #{app_card.preview_url}" }
request = TwitterAds::Request.new(CLIENT, :post, resource, params: tweet_params)
tweet2 = request.perform
# promote tweet with app download card
promoted_tweet2 = TwitterAds::Creative::PromotedTweet.new(account)
promoted_tweet2.line_item_id = line_item.id
promoted_tweet2.tweet_id = tweet2.body[:data][:id]
promoted_tweet2.save
# fetching targeting criteria values (see https://dev.twitter.com/ads/campaigns/targeting)
resource = '/0/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 = '6416b8512febefc9'
targeting_criteria.save
# 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 (see https://dev.twitter.com/ads/analytics/metrics-and-segmentation)
metrics = [:billed_engagements, :billed_follows]
# fetching stats on the instance
line_items.first.stats(metrics)
# fetching stats for multiple line items (see https://dev.twitter.com/ads/analytics/best-practices)
ids = line_items.map { |line_item| line_item.id }
TwitterAds::LineItem.stats(account, ids, metrics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment