[Ruby] Cut off the contents of twitter-ads

In my business, I used twitter-ads to hit the API of the Twitter ad distribution management screen. I'm too new to Ruby and it was pretty tough, so I'll make a note of it.

twitter-ads version is 7.

Generate a client

It doesn't start without making this.

require 'twitter-ads'

# initialize the client
client = TwitterAds::Client.new(

Get an account

Looking at the quick start on github.

# load the advertiser account instance
account = client.accounts('c3won9gy')

There is, but the ID to pass there? What is it? It will be a story. As a result of various investigations

client.accounts.each do |v|
  p v.id

If I did it like this, there was one such thing, so I had to give it the ID that came out there.

Get a list of campaigns held by your account

Use the ʻaccount` obtained above.

account.campaigns.each do |v|
  p "====================="
  p v.id
  p v.reasons_not_servable
  p v.servable
  p v.deleted
  p v.created_at
  p v.updated_at
  p v.name
  p v.funding_instrument_id
  p v.end_time
  p v.start_time
  p v.entity_status
  p v.currency
  p v.standard_delivery
  p v.daily_budget_amount_local_micro
  p v.total_budget_amount_local_micro
  p v.to_delete
  p "====================="

If you do this, a list of campaigns will appear. (If you know how to get all the values in one line without ping one by one like this ...)

If you want to create an object only for a specific campaign,

campaign = account.campaigns('1m5bbbb')

Generate it like this (the ID to be passed is the value that came out in the above v.id)


You can get the campaign name like this.

campaign.name = "MADOKA"

If you do, you can change the campaign name. I'm full of happiness.

Output ad group


This also outputs the ad group held by ʻaccount, so use ʻaccount.

Now you can get a list of ad groups.

Get a list of tailored audiences.

This also uses ʻaccount`.

account.tailored_audiences.each do |v|
  p "==========="
  p v.id
  p v.name
  p v.list_type
  p v.audience_size
  p v.audience_type
  p v.metadata
  p v.partner_source
  p v.reasons_not_targetable
  p v.targetable
  p v.targetable_types
  p "==========="

Up to this point, I have somehow grasped the atmosphere of the acquisition system.

From here, I will write more practical usage.

Bring out a tailored audience associated with your ad group.

Ad groups

line_item = account.line_items('1abcd')

You can get it at.

So, with this acquired line_item,

line_item.targeting_criteria.each do |target|
  if target.targeting_type == 'TAILORED_AUDIENCE' then
    p account.tailored_audiences(target.targeting_value).name

I feel like this.

Unlink tailored audiences associated with ad groups

line_item.targeting_criteria.each do |target|
  if target.targeting_type == 'TAILORED_AUDIENCE' then

Associate a specific tailored audience with a specified ad group

This was a lot of work,

tc = TwitterAds::TargetingCriteria.new(account)
tc.line_item_id    = 'ID of the ad group to be linked'
tc.targeting_value = 'Tailored Audience ID you want to associate(value)'
tc.targeting_type  = 'TAILORED_AUDIENCE'This is fixed and you can put this in

It was like that.

that's all

Regarding tying. Creating a TA (Taylored Audience) doesn't immediately put you in "ready" status, so After creating it, you need to periodically go to see the TA in batch, check that the status is "targetable", and build a logic called linking.

It was a pain to understand how to use this SDK, but once I understood it, all I had to do was write it, so it was fun.

It's like that today.


Recommended Posts

[Ruby] Cut off the contents of twitter-ads
[Ruby] Display the contents of variables
Delete all the contents of the list page [Ruby on Rails]
[Ruby] How to retrieve the contents of a double hash
About the behavior of ruby Hash # ==
[Rails] Check the contents of the object
Replace the contents of the Jar file
[Ruby] See the essence of ArgumentError
Check the contents of the Java certificate store
Check the contents of params with pry
Memo: [Java] Check the contents of the directory
part of the syntax of ruby ​​on rails
Folding and unfolding the contents of the Recyclerview
Ruby from the perspective of other languages
Format the contents of LocalDate with DateTimeFormatter
The contents of the data saved by CarrierWave.
[Ruby On Rails] How to search the contents of params using include?
[Ruby] Summary of class definitions. Master the basics.
JAVA: jar, aar, view the contents of the file
Verify the contents of the argument object with Mockito
Customize how to divide the contents of Recyclerview
[Ruby on Rails] Until the introduction of RSpec
[Delete the first letter of the character string] Ruby
The story of introducing Ajax communication to ruby
Manage the version of Ruby itself with rbenv
Overwrite the contents of config with Spring-boot + JUnit5
I want to var_dump the contents of the intent
[Ruby] "Reference to object" and "Contents of variable"
[Ruby] Code to display the day of the week
I checked the number of taxis with Ruby
[Ruby] Cut out a string using the slice method
Count the number of occurrences of a string in Ruby
[Ruby] The role of subscripts in learning elements in arrays
Docker the development environment of Ruby on Rails project
[Ruby] How to find the sum of each digit
Test the contents of an Excel file with JUnit
Ruby, Nokogiri: Get the element name of the selected node
[Technical memo] About the advantages and disadvantages of Ruby
[Rails] How to get the contents of strong parameters
Java: Use Stream to sort the contents of the collection
[Ruby] Class nesting, inheritance, and the basics of self
Get the URL of the HTTP redirect destination in Ruby
I want to be aware of the contents of variables!
Try using the query attribute of Ruby on Rails
The world of clara-rules (2)
Judgment of the calendar
The world of clara-rules (4)
The world of clara-rules (1)
The world of clara-rules (3)
definition of ruby method
The world of clara-rules (5)
The idea of quicksort
About the [ruby] operator
The idea of jQuery
Review the basic knowledge of ruby that is often forgotten
I took a peek at the contents of Java's HashMap
[Ruby] Questions and verification about the number of method arguments
The idea of cutting off when the error is not resolved
(Ruby on Rails6) Display of the database that got the id of the database
A note about the seed function of Ruby on Rails
The nth and n + 1st characters of a Ruby string