Query data using GAQL
Returns report data using GAQL (Google Ads Query Language). Use get_query_result with WITH(Query='...'); build the query in the Query Builder (select resource type, fields, segments, metrics) and paste the generated SELECT, FROM, ORDER BY into Query.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
--Use Query Builder here https://developers.google.com/google-ads/api/fields/v18/overview_query_builder
--Paste Generated query in Query='here'
SELECT
[campaign.id] as id,
[campaign.name] as name,
[metrics.impressions] as impressions,
[metrics.clicks] as clicks,
[metrics.costMicros] / 1000000 as cost --//API returns Unit in Micro so to get actual value divide by 1000000
FROM get_query_result
WITH(
Filter='$.results[*]'
, Query='SELECT campaign.id, campaign.name, metrics.impressions, metrics.clicks, metrics.cost_micros
FROM campaign
WHERE campaign.status = ''ENABLED''
AND metrics.impressions > 10
ORDER BY campaign.id
--LIMIT 1000
'
--Set different Customer Id here if not supplied in connection settings
--, CustomerId='1112223333'
)
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_GOOGLE_ADS_IN_GATEWAY], '--Use Query Builder here https://developers.google.com/google-ads/api/fields/v18/overview_query_builder
--Paste Generated query in Query=''here''
SELECT
[campaign.id] as id,
[campaign.name] as name,
[metrics.impressions] as impressions,
[metrics.clicks] as clicks,
[metrics.costMicros] / 1000000 as cost --//API returns Unit in Micro so to get actual value divide by 1000000
FROM get_query_result
WITH(
Filter=''$.results[*]''
, Query=''SELECT campaign.id, campaign.name, metrics.impressions, metrics.clicks, metrics.cost_micros
FROM campaign
WHERE campaign.status = ''''ENABLED''''
AND metrics.impressions > 10
ORDER BY campaign.id
--LIMIT 1000
''
--Set different Customer Id here if not supplied in connection settings
--, CustomerId=''1112223333''
)')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_GOOGLE_ADS_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = '--Use Query Builder here https://developers.google.com/google-ads/api/fields/v18/overview_query_builder
--Paste Generated query in Query=''here''
SELECT
[campaign.id] as id,
[campaign.name] as name,
[metrics.impressions] as impressions,
[metrics.clicks] as clicks,
[metrics.costMicros] / 1000000 as cost --//API returns Unit in Micro so to get actual value divide by 1000000
FROM get_query_result
WITH(
Filter=''$.results[*]''
, Query=''SELECT campaign.id, campaign.name, metrics.impressions, metrics.clicks, metrics.cost_micros
FROM campaign
WHERE campaign.status = ''''ENABLED''''
AND metrics.impressions > 10
ORDER BY campaign.id
--LIMIT 1000
''
--Set different Customer Id here if not supplied in connection settings
--, CustomerId=''1112223333''
)'
EXEC (@MyQuery) AT [LS_TO_GOOGLE_ADS_IN_GATEWAY]