ruby Twitter Ads API入门(Ruby)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Twitter Ads API入门(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 = "/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
# 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 = "/1/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 = '/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 = '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)
以上是关于ruby Twitter Ads API入门(Ruby)的主要内容,如果未能解决你的问题,请参考以下文章
我想在 ruby 编程语言中使用 twitter api 以文本而不是对象获取以下列表
如何使用 Ruby on Rails 模拟 twitter api?