Tried below Query it is executing in bigquery but no result delivered. How to fetch the Avg.Time on Screen. Pls. Help
select event_name,
param1.value.string_value as firebase_previous_screen,
param2.value.int_value as engagement_time_msec,
param3.value.string_value as firebase_screen,count(*) as screen_views,
count( distinct user_pseudo_id ) as uniqueusers,
From `<Table>`,
UNNEST(event_params) as param1,
UNNEST(event_params) as param2,
UNNEST(event_params) as param3
where _TABLE_SUFFIX BETWEEN '20220322' AND '20220323'
and event_name = 'user_engagement'
and param1.key='firebase_previous_screen' and
param2.key='engagement_time_msec' and
param3.key='firebase_screen'
group by 1, 2, 3 ,4 order by
engagement_time_msec desc
#ewertonvsilva For your Query Shared. I am getting below error. Pls. find below the screenshot for your kind reference. Pls. help
Related
I'm trying to extract data from Google Analytics, but due to incompatibilities between dimensions and metrics, it was decided to use Google Big Query instead, to obtain the data related to GA4.
I'm struggling to find some metrics/dimensions in Google BigQuery, even searching on the documentation: https://support.google.com/analytics/answer/3437719?hl=en
Google Analytics Dimensions/Metrics:
These are the dimensions and metrics that I've used from google analytics and the ones I can't find in Google Big Query are:
Users
Sessions (I used totals. visits, but I get only NULLs and 1's, while on GA it fills with more numbers)
TransactionsPerSession
CountryIsoCode (In GA it is only the country indicative, for instance, Spain --> ES, but in Big Query, it's the country's complete name. This can be solved, but would be good to have the country code directly from the source)
avgSessionDuration
A great place to get this information is https://www.ga4bigquery.com/
I have copied one of my reports that will provide you with points 1,2,3 & 5. I don't use country but it can be found in the link above
-- subquery to prepare the data
with prep_traffic as (
select
user_pseudo_id,
event_date as date,
count(distinct (ecommerce.transaction_id)) as Transactions,
(select value.int_value from unnest(event_params) where key = 'ga_session_id') as session_id,
max((select value.string_value from unnest(event_params) where key = 'session_engaged')) as session_engaged,
max((select value.int_value from unnest(event_params) where key = 'engagement_time_msec')) as engagement_time_msec,
-- change event_name to the event(s) you want to count
countif(event_name = 'page_view') as event_count,
-- change event_name to the conversion event(s) you want to count
countif(event_name = 'add_payment_info') as conversions,
sum(ecommerce.purchase_revenue) as total_revenue
from
-- change this to your google analytics 4 bigquery export location
`bigquery****.events_*`
where
-- change the date range by using static and/or dynamic dates
_table_suffix between '20230129' and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
group by
user_pseudo_id,
session_id,
event_date)
-- main query
select
count(distinct user_pseudo_id) as users,
count(distinct concat(user_pseudo_id,session_id)) as sessions,
count(distinct case when session_engaged = '1' then concat(user_pseudo_id,session_id) end) as engaged_sessions,
ROUND(safe_divide(count(distinct case when session_engaged = '1' then concat(user_pseudo_id,session_id) end),count(distinct user_pseudo_id)),2) as engaged_sessions_per_user,
ROUND(safe_divide(count(distinct case when session_engaged = '1' then concat(user_pseudo_id,session_id) end),count(distinct concat(user_pseudo_id,session_id))),2) as engagement_rate,
(sum(Transactions)) As transactions,
(sum(Transactions))/ count(distinct concat(user_pseudo_id,session_id)) as TransactionsPerSession,
safe_divide(sum(engagement_time_msec),count(distinct case when session_engaged = '1' then concat(user_pseudo_id,session_id) end)) /count(distinct case when session_engaged = '1' then concat(user_pseudo_id,session_id) end)as avgSessionDuration,
sum(event_count) as event_count,
sum(conversions) as conversions,
ifnull(sum(total_revenue),0) as total_revenue,
date
from
prep_traffic
group by
date
order by
date desc, users desc
Here is my current code to calculate DAE,WAE,MAE:
select event_timestamp as day, Section, users as DAE, SUM(users)
OVER (PARTITION BY Section
ORDER BY event_timestamp
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW) as WAE,
SUM(users)
OVER (PARTITION BY Section
ORDER BY event_timestamp
ROWS BETWEEN 29 PRECEDING AND CURRENT ROW) as MAE
from (
select count(distinct user_pseudo_id) as users, Section, event_timestamp
from
(select distinct *
from
(
select *,
CASE
WHEN param_value = "Names" or param_value = "SingleName" THEN 'Names'
ELSE param_value
END AS Section
from(
select user_pseudo_id, DATE_TRUNC(EXTRACT(DATE from TIMESTAMP_MICROS(event_timestamp)), DAY) as event_timestamp, event_name, params.value.string_value as param_value
from `rayn-deen-app.analytics_317927526.events_*`, unnest(event_params) as params
where (event_name = 'screen_view' and params.key = 'firebase_screen' and (
# Promises
params.value.string_value = "Promises"
# Favourites
or params.value.string_value = "Favourites"
))
group by user_pseudo_id, event_timestamp, event_name, param_value
order by event_timestamp, user_pseudo_id) raw
) base
order by event_timestamp) as events_table
group by Section, event_timestamp
)
The problem is that for WAE,MAE there is repeat counts of the same users happening. So for example user A was a "daily active user" for 4 days that week. Then in the WAE count, it will consider that as 4 users instead of one. So there is a problem of repeat counts which I need to remove somehow.
Im need to know the duration of the sessions one by one of my users, to do that i use bigquery, in the next query i try to get the time, but to get you in all the context:
the param ga_session_id propagate for all the event in a session, then I want to rest the timestamp of the session_start (the start of the session) and the last event with this ga_session_id, that for each ga_session_id.
WITH grps AS (
SELECT event_timestamp, event_name,
(SELECT value.int_value FROM UNNEST(event_params)
WHERE key = "ga_session_id") AS sessionid,
COUNTIF(event_name = 'session_start') OVER (ORDER BY event_timestamp) as grp
FROM `nodal-descent-XXXXX.analytics_XXXXXX.events_intraday_*`
)
SELECT min(event_timestamp), max(event_timestamp),
timestamp_diff(timestamp_micros(max(event_timestamp)),
timestamp_micros(min(event_timestamp)), second) as se
FROM grps
An example of the data i have:
Anyone can help me to complete the query and do that but by each ga_session_id?
If I understand your question, you are looking to add the session id to your current query. If so try the following:
select
ep.value.int_value as ga_session_id
, min(event_timestamp) min_ses
, max(event_timestamp) max_ses
, timestamp_diff(timestamp_micros(max(event_timestamp)), timestamp_micros(min(event_timestamp)), second) as se
from bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_20210131,
UNNEST(event_params) ep
where ep.key='ga_session_id'
group by ga_session_id
order by ga_session_id
I'm not much used to SQL, but on my own I've been able to run this code:
SELECT
event_name,
COUNT(event_name) AS count,
COUNT(event_name) / SUM(COUNT(event_name)) OVER () * 100 AS event_percent
FROM `table_1`
WHERE
event_name IN ('session_start', 'view_item', 'select_item', 'add_to_cart', 'remove_from_cart', 'begin_checkout', 'purchase' )
GROUP BY
event_name
ORDER BY
count DESC
enter image description here
What I'd like to achive is the percentatge of each COUNT divided by the MAX COUNT. Example: purchase / session_start (22 / 1258)
If anyone can help.. I've tried some things but none worked
I guess a CTE would work
WITH prep AS (
SELECT
event_name,
COUNT(event_name) AS cnt,
COUNT(event_name) / SUM(COUNT(event_name)) OVER () * 100 AS event_percent
FROM `table_1`
WHERE
event_name IN ('session_start', 'view_item', 'select_item', 'add_to_cart', 'remove_from_cart', 'begin_checkout', 'purchase' )
GROUP BY
event_name
ORDER BY
count DESC
)
SELECT
*,
cnt / max(cnt) over()
FROM
prep
I would to like get a closed funnel for my X screen views, which are parameters of event screen_view
I have found this very good tutorial - https://medium.com/firebase-developers/how-do-i-create-a-closed-funnel-in-google-analytics-for-firebase-using-bigquery-6eb2645917e1 but it is only for a closed funnel with events.
I would like to get this:
event_name event_param count_users
screen_view screen_name_1 100
screen_view screen_name_2 50
screen_view screen_name_3 20
screen_view screen_name_4 5
What I have tried is to change the provided code in the tutorial to event params, but I got to the point where I have no idea what to do next.
SELECT *,
IF (value.string_value = "screen_name1", user_pseudo_id, NULL) as funnel_1,
IF (value.string_value = "screen_name1" AND next_event = "screen_name2", user_pseudo_id, NULL) AS funnel_2
FROM (
SELECT p.value.string_value, user_pseudo_id , event_timestamp,
LEAD(p.value.string_value, 1) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp) AS next_event
FROM `ProjectName.analytics_XX.events_20190119` as t1, UNNEST(event_params) as p
WHERE (p.value.string_value = "screen_name1" OR p.value.string_value = "screen_name2")
ORDER BY 2,3
LIMIT 100
)
Thanks for any help!
I have found the solution:
SELECT COUNT(DISTINCT funnel_1) as f1_users, COUNT(DISTINCT funnel_2) as f2_users FROM (
SELECT *,
IF (param.value.string_value = "screen_name1", user_pseudo_id, NULL) AS funnel_1,
IF (param.value.string_value = "screen_name1" AND next_screen = "screen_name2", user_pseudo_id, NULL) AS funnel_2
FROM (
SELECT TIMESTAMP_MICROS(event_timestamp), param, user_pseudo_id,
LEAD(param.value.string_value, 1) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp) as next_screen
FROM `ProjectName.analytics_XX.events_20190119`, unnest(event_params) as param
WHERE
event_name = "screen_view" and
param.value.string_value IN ("screen_name1", "screen_name2")
AND _TABLE_SUFFIX BETWEEN '20190205' AND '20190312'
)
)