Skip to content

Instantly share code, notes, and snippets.

@jhnvdw
Last active May 31, 2022 00:27
Show Gist options
  • Save jhnvdw/3d70ea00d2514f227356c62a154b4bd4 to your computer and use it in GitHub Desktop.
Save jhnvdw/3d70ea00d2514f227356c62a154b4bd4 to your computer and use it in GitHub Desktop.
SELECT
-- Sessions (metric)
COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))) AS Sessions,
-- Bounces (metric)
COUNT(DISTINCT
CASE
WHEN totals.bounces = 1 THEN CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))
ELSE
NULL
END
) AS Bounces,
-- Bounce Rate (metric)
COUNT(DISTINCT
CASE
WHEN totals.bounces = 1 THEN CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))
ELSE
NULL
END
) / COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))) AS Bounce_Rate,
-- Average Session Duration (metric)
SUM(totals.timeOnSite) / COUNT(DISTINCT CONCAT(fullVisitorId, CAST(visitStartTime AS STRING))) AS Average_Session_Duration
FROM
`bigquery-public-data.google_analytics_sample.ga_sessions_*`
WHERE
_table_suffix BETWEEN '20160801'
AND FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
AND totals.visits = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment