The sample queries in this page apply to the BigQuery event export data for Google Analytics 4. See BigQuery cookbook for Universal Analytics if you are looking for the same resource for Universal Analytics.
All queries listed here use sample datasets and should produce valid results. To
use your own Google Analytics property's BigQuery event export data, look for
the comment -- Replace table
in each query and replace the sample dataset id.
You can find your dataset Id by going navigating to your BigQuery export project
in the BigQuery UI. For example, if your BigQuery export datasets Id is my-
first-gcp-project:analytics_28239234
, then replace:
-- Replace table
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
with:
-- Replace table
`my-first-gcp-project.analytics_28239234.events_*`
Query a specific date range
To query a specific date range from a BigQuery event export dataset, use the _TABLE_SUFFIX pseudo column in the WHERE clause of your query. For more info, view Filtering selected tables using _TABLE_SUFFIX.
For example, the following query counts unique events by date and by event name for a specifc period of days and selected events:
-- Example: Query a specific date range for selected events.
--
-- Counts unique events by date and by event name for a specifc period of days and
-- selected events(page_view, session_start, and purchase).
SELECT
event_date,
event_name,
COUNT(*) AS event_count
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE
event_name IN ('page_view', 'session_start', 'purchase')
-- Replace date range.
AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201202'
GROUP BY 1, 2;
User count and new user count
- To get the total user count, count the number of distinct
user_id
. However, if your Google Analytics client does not send back auser_id
with each hit or if you are unsure, count the number of distinctuser_pseudo_id
. - For new users, you can take the same count approach described above but for
the following values of
event_name
:
-- Example: Get 'Total User' count and 'New User' count.
WITH
UserInfo AS (
SELECT
user_pseudo_id,
MAX(IF(event_name IN ('first_visit', 'first_open'), 1, 0)) AS is_new_user
-- Replace table name.
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
-- Replace date range.
WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20201130'
GROUP BY 1
)
SELECT
COUNT(*) AS user_count,
SUM(is_new_user) AS new_user_count
FROM UserInfo;
Average number of transactions per purchaser
The following query shows the average number of transactions per purchaser.
-- Example: Average number of transactions per purchaser.
SELECT
COUNT(*) / COUNT(DISTINCT user_pseudo_id) AS avg_transaction_per_purchaser
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE
event_name IN ('in_app_purchase', 'purchase')
-- Replace date range.
AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201231';
Values for a specific event name
The following query shows the event_timestamp
for all
purchase
events and the associated event parameter
values:
-- Example: Query values for a specific event name.
--
-- Queries the individual timestamps and values for all 'purchase' events.
SELECT
event_timestamp,
(
SELECT COALESCE(value.int_value, value.float_value, value.double_value)
FROM UNNEST(event_params)
WHERE key = 'value'
) AS event_value
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE
event_name = 'purchase'
-- Replace date range.
AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201202';
The previous query can be modified to show the total of event parameter values instead of a list:
-- Example: Query total value for a specific event name.
--
-- Queries the total event value for all 'purchase' events.
SELECT
SUM(
(
SELECT COALESCE(value.int_value, value.float_value, value.double_value)
FROM UNNEST(event_params)
WHERE key = 'value'
))
AS event_value
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE
event_name = 'purchase'
-- Replace date range.
AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201202';
Top 10 items added to cart
The following query shows the top 10 item added to cart by the most number of users.
-- Example: Top 10 items added to cart by most users.
SELECT
item_id,
item_name,
COUNT(DISTINCT user_pseudo_id) AS user_count
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_web_ecommerce.events_*`, UNNEST(items)
WHERE
-- Replace date range.
_TABLE_SUFFIX BETWEEN '20201101' AND '20210131'
AND event_name IN ('add_to_cart')
GROUP BY
1, 2
ORDER BY
user_count DESC
LIMIT 10;
Average number of pageviews by purchaser type (purchasers vs non-purchasers)
The following query shows the average number of pageviews purchaser type (purchasers vs non-purchasers) of users:
-- Example: Average number of pageviews by purchaser type.
WITH
UserInfo AS (
SELECT
user_pseudo_id,
COUNTIF(event_name = 'page_view') AS page_view_count,
COUNTIF(event_name IN ('in_app_purchase', 'purchase')) AS purchase_event_count
-- Replace table name.
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
-- Replace date range.
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201202'
GROUP BY 1
)
SELECT
(purchase_event_count > 0) AS purchaser,
COUNT(*) AS user_count,
SUM(page_view_count) AS total_page_views,
SUM(page_view_count) / COUNT(*) AS avg_page_views,
FROM UserInfo
GROUP BY 1;
Sequence of pageviews
The following query shows the sequence of pageviews made by users during unique sessions:
-- Example: Sequence of pageviews.
SELECT
user_pseudo_id,
event_timestamp,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location')
AS page_location,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_title') AS page_title
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE
event_name = 'page_view'
-- Replace date range.
AND _TABLE_SUFFIX BETWEEN '20201201' AND '20201202'
ORDER BY
user_pseudo_id,
ga_session_id,
event_timestamp ASC;
Event parameter list
The following query lists all event parameters appearing in your dataset:
-- Example: List all available event parameters and count their occurrences.
SELECT
EP.key AS event_param_key,
COUNT(*) AS occurrences
FROM
-- Replace table name.
`bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`, UNNEST(event_params) AS EP
WHERE
-- Replace date range.
_TABLE_SUFFIX BETWEEN '20201201' AND '20201202'
GROUP BY
event_param_key
ORDER BY
event_param_key ASC;