The Insider's Playbook to Mastering Amazon Marketing Cloud?SQL
Getting started with Amazon Ads Marketing Cloud requires an understanding of the data and how to write AMC SQL.
Ultimately, the goal of tapping into AMC data is to refine advertising strategy, have a deeper insight into AMC audiences and segments, and create a feedback loop where you have the capabilities to create actionable insights rapidly to optimize advertising campaigns.
This post demonstrates various examples of AMC SQL queries and how to gain free access to an AMC SQL Data Analyst AI copilot.
Using An AMC AI Data?Copilot!
Most people interested in AMC are not data scientists or software engineers. Asa result, it can be a struggle to build the skills needed to author structured query language (SQL) queries against complex AMC data.
We released “Chatlytics,” a free Amazon Marketing Cloud data analyst AI copilot to solve those challenges. It is trained on all things Amazon Marketing Cloud SQL. Using the Chatlytics data analyst copilot is as simple as starting a conversation about your AMC query or dataset.?When you're stuck hunting down a SQL error or exploring new ways to create insights, ask the AMC AI Data Analyst Copilot*.?
*The SQL, analysis, and visualizations in this post were all developed in collaboration with the AMC SQL AI Copilot!
Sponsored Ads Traffic AMC?SQL
AMC covers Amazon DSP, Amazon Sponsored Ads (like Sponsored Products), Amazon Attribution, and other AMC data sets. This example is a query for retrieving impressions, clicks, and spending metrics for Sponsored Products and Sponsored brand campaigns from the sponsored_ads_traffic table:
SELECT
ad_product_type,
targeting,
customer_search_term,
match_type,
SUM(spend)/100000000 AS total_cost_dollars,
((SUM(spend)/100000000)/SUM(impressions))*1000 AS avg_cpm,
SUM(impressions) AS impressions,
SUM(clicks) AS clicks,
(SUM(clicks)/SUM(impressions)) AS ctr
FROM
sponsored_ads_traffic
WHERE
match_type IN('PHRASE', 'BROAD', 'EXACT')
GROUP BY
ad_product_type, targeting, customer_search_term, match_type
This query calculates total spend in dollars, the average cost per thousand impressions (avg_cpm), total impressions, total clicks, and click-through rate (ctr) for Sponsored Products and Sponsored Brands campaigns. The query filters data for specific match types (PHRASE, BROAD, EXACT) and groups the results by ad_product_type, targeting, customer_search_term, and match_type?.
Based on the Openbridge knowledge base, here is a collection of instructional AMC SQL queries designed to cover a range of fundamental concepts and operations in AMC SQL.
These queries offer a structured approach to learning AMC SQL, focusing on different aspects such as aggregation, join operations, conditional logic, and advanced analytics.
Aggregate Impressions and Clicks by Campaign
SELECT
SUM(c.clicks) AS total_clicks,
COALESCE(SUM(a.conversions), 0) AS total_conversions
FROM dsp_clicks c
LEFT JOIN amazon_attributed_events_by_conversion_time a ON c.request_tag = a.request_tag
This query aggregates total impressions and clicks for each campaign in the DSP impressions table. It demonstrates basic aggregation and grouping functionalities.
DSP Clicks advertiser performance, campaign details, and cost efficiency
Here's an instructional AMC SQL example query for analyzing DSP (Demand Side Platform) clicks data focusing on advertiser performance, campaign details, and cost efficiency. This query aims to aggregate click data by advertiser, campaign, and device type, offering insights into the effectiveness of different campaigns and the devices on which they perform best.
SELECT
advertiser,
campaign,
device_type,
COUNT(clicks) AS total_clicks, -- Aggregating the total number of clicks
SUM(click_cost)/100000 AS total_click_cost_dollars, -- Convert click cost from millicents to dollars
AVG(click_cost)/100 AS avg_click_cost_cents, -- Calculate average click cost in cents
MAX(click_cost)/100 AS max_click_cost_cents, -- Identify the maximum click cost in cents for benchmarking
MIN(click_cost)/100 AS min_click_cost_cents -- Find the minimum click cost in cents for optimization opportunities
FROM
dsp_clicks
GROUP BY
advertiser,
campaign,
device_type
This query performs the following operations:
DSP Impresssion and DSP?Clicks
Here's an AMC SQL example for an analysis that combines DSP impressions and clicks data to evaluate campaign performance, including impressions, clicks, and click-through rate (CTR). This query joins the dsp_impressions and dsp_clicks tables on campaign and advertiser IDs, providing a comprehensive view of campaign effectiveness across different devices and browsers.
WITH impressions AS (
SELECT
advertiser_id,
campaign_id,
device_type,
COUNT(impression_date) AS total_impressions -- Assuming 'impression_date' can represent each unique impression
FROM
dsp_impressions
GROUP BY
advertiser_id,
campaign_id,
device_type
),
clicks AS (
SELECT
advertiser_id,
campaign_id,
COUNT(click_date) AS total_clicks -- Assuming 'click_date' can represent each unique click
FROM
dsp_clicks
GROUP BY
advertiser_id,
campaign_id
),
combined_data AS (
SELECT
i.advertiser_id,
i.campaign_id,
i.device_type,
i.total_impressions,
COALESCE(c.total_clicks, 0) AS total_clicks
FROM
impressions i
LEFT JOIN
clicks c ON i.advertiser_id = c.advertiser_id AND i.campaign_id = c.campaign_id
)
SELECT
advertiser_id,
campaign_id,
device_type,
total_impressions,
total_clicks,
CASE
WHEN total_impressions > 0 THEN ROUND((total_clicks / total_impressions) * 100, 2)
ELSE 0
END AS ctr
FROM
combined_data
In this query, we:
This approach ensures compliance with AMC’s SQL requirements and provides an analysis framework for DSP campaign performance across different device types.
To extend the approach to include dsp_views, dsp_impressions and dsp_clicks, creating a more comprehensive analysis of DSP data, we'll introduce another CTE for views. This will aggregate total views alongside impressions and clicks for each campaign and advertiser. This updated query will provide insights into how many impressions and clicks each campaign received and how many impressions were viewable.
WITH impressions AS (
SELECT
advertiser_id,
campaign_id,
device_type,
COUNT(impression_date) AS total_impressions -- Assuming 'impression_date' represents each unique impression
FROM
dsp_impressions
GROUP BY
advertiser_id,
campaign_id,
device_type
),
clicks AS (
SELECT
advertiser_id,
campaign_id,
COUNT(click_date) AS total_clicks -- Assuming 'click_date' represents each unique click
FROM
dsp_clicks
GROUP BY
advertiser_id,
campaign_id
),
views AS (
SELECT
advertiser_id,
campaign_id,
COUNT(event_date) AS total_views -- Assuming 'event_date' can represent each unique view
FROM
dsp_views
GROUP BY
advertiser_id,
campaign_id
),
combined_data AS (
SELECT
i.advertiser_id,
i.campaign_id,
i.device_type,
i.total_impressions,
COALESCE(c.total_clicks, 0) AS total_clicks,
COALESCE(v.total_views, 0) AS total_views
FROM
impressions i
LEFT JOIN
clicks c ON i.advertiser_id = c.advertiser_id AND i.campaign_id = c.campaign_id
LEFT JOIN
views v ON i.advertiser_id = v.advertiser_id AND i.campaign_id = v.campaign_id
)
SELECT
advertiser_id,
campaign_id,
device_type,
total_impressions,
total_clicks,
total_views,
CASE
WHEN total_impressions > 0 THEN ROUND((total_clicks / total_impressions) * 100, 2)
ELSE 0
END AS ctr,
CASE
WHEN total_impressions > 0 THEN ROUND((total_views / total_impressions) * 100, 2)
ELSE 0
END AS view_rate
FROM
combined_data
This query includes:
The query offers a multi-dimensional view of DSP campaign performance, incorporating the engagement metrics (clicks) and the visibility of ads (views) across different devices. This enhanced analysis allows advertisers to understand which campaigns generate interactions and which are effectively reaching and being seen by the audience.
Calculating DSP Campaign and Sponsored Display Campaign?Costs
Combining the principles from the DSP campaigns and Sponsored Display campaigns, the goal is to create a comprehensive query that calculates the costs for both types of campaigns in one unified view. This query will incorporate conversion from millicents and microcents to dollars for DSP ads and Sponsored Display ads, respectively, while accounting for the different billing strategies of CPC (Cost Per Click) and vCPM (Cost per thousand viewable impressions) for Sponsored Display.
领英推荐
-- DSP Campaigns Cost Calculation
WITH dsp_campaign_costs AS (
SELECT
CAST('DSP Campaign' AS VARCHAR(255)) AS campaign_type,
CAST(campaign_id AS VARCHAR(255)) AS campaign_id,
CAST(campaign AS VARCHAR(255)) AS campaign_name,
CAST(currency_name AS VARCHAR(255)) AS currency_name,
CAST(currency_iso_code AS VARCHAR(255)) AS currency_iso_code,
CAST(SUM(impression_cost) / 100000.0 AS DECIMAL(10, 2)) AS impression_cost_dollars,
CAST(SUM(total_cost) / 100000.0 AS DECIMAL(10, 2)) AS total_cost_dollars
FROM
dsp_impressions
GROUP BY
campaign_id, campaign, currency_name, currency_iso_code
),
-- Sponsored Display Campaigns Cost Calculation
sponsored_display_costs AS (
SELECT
CAST('Sponsored Display Campaign' AS VARCHAR(255)) AS campaign_type,
CAST(NULL AS VARCHAR(255)) AS campaign_id, -- Explicitly casting NULL for campaign_id
CAST(campaign AS VARCHAR(255)) AS campaign,
CAST(NULL AS VARCHAR(255)) AS currency_name, -- Assuming uniform currency, explicitly casting NULL
CAST(NULL AS VARCHAR(255)) AS currency_iso_code, -- Assuming uniform currency, explicitly casting NULL
CAST(0.0 AS DECIMAL(10,2)) AS impression_cost_dollars, -- Placeholder for consistent column structure
CAST(SUM(spend) / 100000000.0 AS DECIMAL(10, 2)) AS spend_dollars
FROM
sponsored_ads_traffic
WHERE
ad_product_type = 'sponsored_display'
GROUP BY
campaign
)
-- Final Selection with explicit casts to ensure data type consistency across UNION ALL
SELECT
campaign_type,
campaign_id,
campaign_name,
currency_name,
currency_iso_code,
impression_cost_dollars,
total_cost_dollars
FROM
dsp_campaign_costs
UNION ALL
SELECT
campaign_type,
campaign_id,
campaign,
currency_name,
currency_iso_code,
impression_cost_dollars,
spend_dollars AS total_cost_dollars -- Matching the column name and data type in the SELECT above
FROM
sponsored_display_costs
ORDER BY
campaign_type, campaign_name;
Ad-attributed Branded?Searches
Calculating the branded search rate and cost per branded search streamlines the analysis of ad-attributed branded searches. The objective is to create a comprehensive view that captures the branded search activity and offers insights into its efficiency and cost-effectiveness. The optimization simplifies data handling and ensures all calculations are done within a cohesive query structure.
WITH branded_searches AS (
SELECT
ae.campaign_id,
ae.campaign,
SUBSTRING(ae.tracked_item FROM 9) AS keyword, -- Extracting the keyword explicitly
COUNT(ae.conversions) AS number_of_branded_searches -- Counting conversions explicitly
FROM
amazon_attributed_events_by_conversion_time ae
WHERE
ae.tracked_item LIKE 'keyword%' -- Ensuring LIKE is used for pattern matching
GROUP BY
ae.campaign_id,
ae.campaign,
SUBSTRING(ae.tracked_item FROM 9)
),
campaign_costs AS (
SELECT
di.campaign_id,
di.campaign,
SUM(di.impressions) AS total_impressions,
SUM(di.impression_cost) / 100000.0 AS total_impression_cost_dollars -- Converting millicents to dollars
FROM
dsp_impressions di
GROUP BY
di.campaign_id,
di.campaign
),
-- Merging branded searches with campaign costs for analysis
campaign_analysis AS (
SELECT
cc.campaign_id AS campaign_id,
cc.campaign AS campaign_name,
bs.keyword AS branded_keyword,
cc.total_impressions AS impressions,
cc.total_impression_cost_dollars AS impression_cost_dollars,
COALESCE(bs.number_of_branded_searches, 0) AS branded_searches_count
FROM
campaign_costs cc
LEFT JOIN
branded_searches bs ON cc.campaign_id = bs.campaign_id
)
SELECT
campaign_id,
campaign_name,
branded_keyword,
impressions,
impression_cost_dollars,
branded_searches_count,
CASE
WHEN impressions > 0 THEN
CAST(branded_searches_count AS FLOAT) / CAST(impressions AS FLOAT)
ELSE 0.0
END AS branded_search_rate,
CASE
WHEN branded_searches_count > 0 THEN
impression_cost_dollars / CAST(branded_searches_count AS FLOAT)
ELSE 0.0
END AS cost_per_branded_search
FROM
campaign_analysis
This approach streamlines the analysis process and ensures that the insights derived from the DSP campaign performance regarding branded searches are accurate, comprehensive, and actionable, aligning with the intended use case of optimizing analysis for brand search.
From AMC SQL to AMC?Insights
What can you do with the results of AMC SQL queries? A lot! Here are just a few examples of the types of analysis you can undertake, including data analysis and visualization, to optimize advertising strategies on the same result set.
CTR Performance by Device?Type
The AMC SQL results allow us to calculate the average CTR for each device type across all campaigns to identify which devices yield higher engagement. This could help focus advertising efforts on the most effective devices.
The analysis reveals each device type's average Click-Through Rate (CTR), indicating significant variations across different devices. Here’s a summary of the findings, sorted from highest to lowest average CTR:
These results suggest that advertising on PCs, TVs, and SetTopBoxes yields the highest engagement in terms of CTR, while Phones and Connected Devices have the lowest. Advertising strategies might be optimized by allocating more resources to the higher-performing device types or by exploring ways to improve engagement on the underperforming ones.
Next, here you can visualize these findings with a bar chart to better illustrate the differences in CTR across device types.
The bar chart visually represents the average Click-Through Rate (CTR) by device type, highlighting the significant variance in CTR across different devices. PCs, TVs, and SetTopBoxes show the highest average CTR, suggesting these platforms are more effective for engaging users with advertisements. Conversely, Phones and Connected Devices exhibit lower CTRs, indicating potential areas for optimization or strategy adjustment.
This analysis suggests focusing advertising efforts on the higher-performing devices or investigating strategies to enhance engagement on platforms with lower CTRs. Adjusting creative content, ad placement, or targeting criteria could improve performance on underperforming devices.
Impressions and Clicks Relationship
The outputs of the AMC SQL allow us to investigate the relationship between the number of impressions and clicks across different device types. This could identify if more impressions always lead to more clicks or if there’s a saturation point beyond which additional impressions don’t translate into proportionally more clicks.
Scatter plots for each device type, with total impressions on the X-axis and total clicks on the Y-axis, could help visualize this relationship. Adding a trend line would make it easier to see if the relationship is linear or if diminishing returns are evident.
The scatter plots illustrate the relationship between total impressions and clicks for each device type. Here are some observations:
These insights can help inform advertising strategies by identifying optimal levels of ad exposure across different devices and adjusting campaign efforts to maximize engagement without overspending on impressions that do not convert to clicks.
Campaign Performance Analysis
Calculate each campaign's key performance indicators, such as impressions, clicks, and average CTR. This can help identify high-performing campaigns and understand the characteristics that contribute to their success.
The dashboard-like visualization presents the performance metrics of the top 10 campaigns, focusing on Total Impressions, Total Clicks, and Average Click-Through Rate (CTR). This comparative view allows for a more straightforward analysis of what makes these campaigns successful and offers insights into optimizing future advertising strategies:
Analyzing these metrics together provides a comprehensive view of campaign performance, helping identify strengths to replicate and areas for improvement. For example, campaigns with high impressions but lower CTRs may need to refine targeting or creative content, while those with high CTRs demonstrate successful engagement strategies worth emulating.
Amazon Ads Partners #amazonmarketingcloud #amazon #analytics