Skip to content

Instantly share code, notes, and snippets.

View markrittman's full-sized avatar

Mark Rittman markrittman

View GitHub Profile
@markrittman
markrittman / ga4_page_views.sql
Created July 18, 2021 14:07
GA4 Individual page views with device and geography information SQL query
SELECT
event_timestamp,
(
SELECT
value.string_value
FROM
UNNEST(event_params)
WHERE
event_name = 'page_view'
AND key = 'page_location') AS page_path,
@markrittman
markrittman / ga4_visitor_journey.sql
Last active July 11, 2023 10:01
User Page View Journey for GA4
SELECT
user_pseudo_id,
geo.city,
TIMESTAMP_MICROS(MIN(event_timestamp)) AS session_start_ts,
traffic_source.name AS channel,
traffic_source.medium AS medium,
traffic_source.source AS source,
(
SELECT
value.int_value
@markrittman
markrittman / ga4_attribution_model.sql
Last active February 2, 2023 17:54
First-Click, Last-Click, Even-Click and Time Decay Attribution for GA4
/*
First-Click, Last-Click, Even-Click and Time Decay Attribution for GA4
Replace "event_name in ('Contact Us','Contact Us Clicked','CTA Pressed','Form Submitted')" with the name(s) of your conversion event in lines 54, 57, 61 and 76
*/
WITH
events AS (
SELECT
event_timestamp as event_ts,
user_pseudo_id AS user_pseudo_id,
user_id,
@markrittman
markrittman / rs_segment_analytics.js
Created July 15, 2021 19:41
JS Snippet for loading segment/RS Javascript tracking scripts together (the SEGMENT_WRITE_KEY RUDDERSTACK_WRITE_KEY DATA_PLANE_URL need to be replaced, and you can remove comments and minify as needed)
<script type="text/javascript">
(function(){
// Create a queue, but don't obliterate an existing one!
var analytics = window.analytics = window.analytics || [];
// If the real analytics.js is already on the page return.
if (analytics.initialize) return;
// If the snippet was invoked already show an error.
if (analytics.invoked) {
if (window.console && console.error) {
console.error('Segment snippet included twice.');
SELECT
date_trunc(date(creation_time),MONTH) as billing_month,
user_email,
query,
count(distinct job_id) as query_runs,
sum(total_bytes_billed) as total_bytes_billed,
SUM(total_bytes_billed)/1e12*5 costInDollars
FROM
`region-eu`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE
```SELECT
date_trunc(date(creation_time),MONTH) as billing_month,
count(distinct job_id) as query_runs,
sum(total_bytes_billed) as total_bytes_billed,
SUM(total_bytes_billed)/1e12*5 costInDollars
FROM
`region-eu`.INFORMATION_SCHEMA.JOBS_BY_PROJECT
WHERE
date_trunc(date(creation_time),MONTH) = '2021-04-01'
AND job_type = "QUERY"
https://LOOKER_DOMAIN.looker.com/explore/system__activity/history?fields=history.dashboard_run_count,history.average_runtime,history.completed_week,dashboard.title&pivots=history.completed_week&fill_fields=history.completed_week&f[history.dashboard_session]=-NULL&f[history.completed_week]=4+weeks&f[dashboard.title]=-NULL&sorts=history.completed_week+desc,impact+desc+1&limit=500&vis=%7B%22show_view_names%22%3Afalse%2C%22show_row_numbers%22%3Atrue%2C%22transpose%22%3Afalse%2C%22truncate_text%22%3Atrue%2C%22hide_totals%22%3Afalse%2C%22hide_row_totals%22%3Afalse%2C%22size_to_fit%22%3Atrue%2C%22table_theme%22%3A%22white%22%2C%22limit_displayed_rows%22%3Afalse%2C%22enable_conditional_formatting%22%3Atrue%2C%22header_text_alignment%22%3A%22left%22%2C%22header_font_size%22%3A%2212%22%2C%22rows_font_size%22%3A%2212%22%2C%22conditional_formatting_include_totals%22%3Afalse%2C%22conditional_formatting_include_nulls%22%3Afalse%2C%22color_application%22%3A%7B%22collection_id%22%3A%22legacy%22%2C%22palette_id%22%3A%22looker
https://CLIENT_DOMAIN.looker.com/explore/system__activity/history?fields=look.title,history.completed_week,history.query_run_count,history.average_runtime&pivots=history.completed_week&fill_fields=history.completed_week&f[history.completed_week]=4+weeks&f[look.title]=-EMPTY&sorts=history.query_run_count+desc+0,history.completed_week&limit=500&vis=%7B%22show_view_names%22%3Afalse%2C%22show_row_numbers%22%3Atrue%2C%22transpose%22%3Afalse%2C%22truncate_text%22%3Atrue%2C%22hide_totals%22%3Afalse%2C%22hide_row_totals%22%3Afalse%2C%22size_to_fit%22%3Atrue%2C%22table_theme%22%3A%22white%22%2C%22limit_displayed_rows%22%3Afalse%2C%22enable_conditional_formatting%22%3Atrue%2C%22header_text_alignment%22%3A%22left%22%2C%22header_font_size%22%3A%2212%22%2C%22rows_font_size%22%3A%2212%22%2C%22conditional_formatting_include_totals%22%3Afalse%2C%22conditional_formatting_include_nulls%22%3Afalse%2C%22show_sql_query_menu_options%22%3Afalse%2C%22show_totals%22%3Atrue%2C%22show_row_totals%22%3Atrue%2C%22series_cell_visualization
@markrittman
markrittman / list_dataset_partitioning.sql
Created June 24, 2021 20:22
List all datasets with partitioned tables, broken-down by partitioning type
SELECT
TABLE_SCHEMA,
count(distinct table_name) as table_count,
CASE
WHEN IS_SYSTEM_DEFINED = 'YES' THEN 'Ingestion-Time'
WHEN data_type = 'TIMESTAMP' THEN concat('Time-unit column')
WHEN data_type = 'INT64' THEN concat('Integer Range')
END
AS partitioning_type
FROM
@markrittman
markrittman / looker_query_history.sql
Created February 10, 2021 22:49
BiqQuery StandardSQL query to return Looker query history using Stitch Looker (v1) Integration (https://www.stitchdata.com/docs/integrations/saas/looker)
WITH
history AS (
SELECT
*
FROM (
SELECT
h.query_id,
h.history_created_date AS query_ts,
h.user_id AS user_id,
h.history_query_run_count AS query_count,