← Back to list

How to fetch GCLID(Google Click ID) Using the Google Ads v13 API

To get Google Click ID(GCLID) using the Google Ads API v13, you can use the “click_view” resource. Here are the steps to get started:

Himanshu Sharma · 2023-05-13 06:20 · 26 claps · 2.2 min read
#google-ads #adsense #marketing #google-ads-api #ruby
Open on Medium ↗
Wiki topics: ECO · Economy · General DIG · Digital Marketing MKT · Marketing · General

How to fetch GCLID(Google Click ID) Using the Google Ads v13 API

Google Ads V13 API

Google Ads V13 API

To get Google Click ID(GCLID) using the Google Ads API v13, you can use the “click_view” resource. Here are the steps to get started:

  1. Set up your development environment and obtain API credentials from the Google Ads API console.
  2. Install the Google Ads API Ruby client library using the following command:
gem install google-ads-ruby # For quering the google ads database
gem install googleauth # For generating the refresh token
  1. You can find the CLIENT_ID, CLIENT_SECRET by creating a new Google Cloud project and enabling the Google Ads API.

  2. You can generate the refresh token using the following snippet.

require 'google/ads/google_ads'
require 'googleauth'
require 'googleauth/stores/file_token_store'

# Replace with your own values
CLIENT_ID = 'XXXXXXXXXXXXXXXXX.apps.googleusercontent.com'
CLIENT_SECRET = 'XXXXXXXXXXXXXXXXX'
REFRESH_TOKEN_PATH = 'refresh_token.yml'
# Set up OAuth 2.0 credentials
scopes = ['https://www.googleapis.com/auth/adwords']
client_id = Google::Auth::ClientId.new(CLIENT_ID, CLIENT_SECRET)
token_store = Google::Auth::Stores::FileTokenStore.new(file: REFRESH_TOKEN_PATH)
authorizer = Google::Auth::UserAuthorizer.new(client_id, scopes, token_store)
# Authorize the user and get an access token
user_id = 'your_email_id@example.com'
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
  url = authorizer.get_authorization_url(
    base_url: 'urn:ietf:wg:oauth:2.0:oob', access_type: 'offline'
  )
  puts "Open the following URL in your browser and authorize the application:"
  puts url
  puts "Enter the authorization code:"
  code = gets.chomp
  credentials = authorizer.get_and_store_credentials_from_code(
    user_id: user_id, code: code, base_url: 'urn:ietf:wg:oauth:2.0:oob'
  )
end
# Save the refresh token for future use
puts "Saving refresh token to #{REFRESH_TOKEN_PATH}"
File.open(REFRESH_TOKEN_PATH, 'w') do |file|
  file.write(credentials.to_json)
end
puts "Copy the refresh token : #{credentials.refresh_token}"
  1. To generate a developer_token in a Google Ads Manager Account, you need to have administrative access to the account and follow the steps to request a new token. Once obtained, you can use the developer_token to make requests to the Google Ads API for the associated account.

  2. Import the necessary libraries and initialize the Google Ads API client.

require 'google/ads/google_ads'  
# Replace with your own values
CLIENT_ID = 'XXXXXXXXXXXXXXX.apps.googleusercontent.com'
CLIENT_SECRET = 'XXXXXXXXXXXXXXX'
REFRESH_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
DEVELOPER_TOKEN = "XXXXXXXXXXXXXXX" 
LOGIN_CUSTOMER_ID = '0000000000' #ID of your manager account
CUSTOMER_ID = '0000000000' #ID of your customer google ad account
client = Google::Ads::GoogleAds::GoogleAdsClient.new do |config|
  config.developer_token = DEVELOPER_TOKEN
  config.client_id = CLIENT_ID
  config.client_secret = CLIENT_SECRET
  config.refresh_token = REFRESH_TOKEN
  config.login_customer_id = LOGIN_CUSTOMER_ID
  config.logger = Logger.new(STDOUT)
end
  1. Build a query to retrieve the click and view performance data. you can create the query with the help of Google Ads v13 Query Builder.
PER_PAGE = 10
query = "SELECT click_view.gclid FROM click_view WHERE segments.date = '2023-04-05'"
click_response = client.service.google_ads.search(
  customer_id: CUSTOMER_ID, #ID of your customer google ad account
  query: query,
  page_size: PAGE_SIZE
)
  1. Process the response and extract the relevant data
click_response.each do |row|
  puts "GCLID: #{row.click_view.gclid}"
end

By following the above steps, you can fetch the GCLID using the Google Ads v13 API. This explanation is done by using ruby programming language, any other preferred programming language can be used to do the same.

I’m Himanshu Sharma, currently employed in the development of small and medium-sized web applications using Ruby, React, HTML, CSS, and JS. I specialize in incorporating React JS for features and coding interactive layouts. In my free time, I enjoy watching OTT movies.


메타데이터
post_id
6cb89f5b066d
slug
how-to-fetch-gclid-google-click-id-using-the-google-ads-v13-api-6cb89f5b066d
url
https://medium.com/@iamhimanshu/how-to-fetch-gclid-google-click-id-using-the-google-ads-v13-api-6cb89f5b066d
canonical_url
https://medium.com/@iamhimanshu/how-to-fetch-gclid-google-click-id-using-the-google-ads-v13-api-6cb89f5b066d
author_url
https://medium.com/@iamhimanshu
status
ok
fetched_at
2026-07-07 06:42:44