Skip to content

Instantly share code, notes, and snippets.

View markrittman's full-sized avatar

Mark Rittman markrittman

View GitHub Profile
@markrittman
markrittman / user_journey_with_inferred_identity.sql
Created May 29, 2022 23:41
User journey stitching with inferred identify for off-platform activity
with events as (
select
anonymous_id,
user_id,
cast(null as string) as name,
cast(null as string) as email,
timestamp,
'page_view' as event_type,
concat(split(context_ip,'.')[safe_offset(0)],'.***.***.',split(context_ip,'.')[safe_offset(3)]) as context_ip,
context_page_path,
@markrittman
markrittman / wordpress_search_keyword_stats.sql
Created December 3, 2022 20:37
Return search keyword stats over time for blog, podcast and marketing pages in our Wordpress site
WITH wordpress_posts AS (SELECT * FROM
(
SELECT
p.post_title AS Post_Title,
'Content' AS post_type,
t.name AS Category,
p.post_date AS Post_Date,
p.post_name AS Post_Name,
p.comment_count AS comment_count,
p.post_excerpt
@markrittman
markrittman / google_search_console_weekly_stats.view.lkml
Created December 2, 2022 20:31
LookML view containing Google Search Console keyword page search stats data, formatted to join to Wordpress wp_posts.post_name column
view: google_search_console_weekly_stats {
derived_table: {
sql: WITH
page_keyword_performance AS (
SELECT
CASE
WHEN page = 'https://rittmananalytics.com/' THEN 'home'
ELSE
COALESCE(LOWER(ARRAY_REVERSE( SPLIT(RTRIM(page,'/'),'/'))[SAFE_OFFSET(0)]),'home')
END
@markrittman
markrittman / rudderstack_weekly_page_stats.view.lkml
Created December 2, 2022 20:24
LookML view that aggregates Rudderstack page traffic data and formats the page URL for joining to Wordpress wp_posts.post_name column
view: rudderstack_weekly_page_stats {
derived_table: {
sql: SELECT
title,
CASE
WHEN path = '/' THEN 'home'
ELSE
COALESCE(LOWER(ARRAY_REVERSE( SPLIT(RTRIM(path,'/'),'/'))[SAFE_OFFSET(0)]),'home')
END
AS page_name,
@markrittman
markrittman / wordpress_posts.view.lkml
Created December 1, 2022 23:19
Example LookML view to create Bitnami Wordpress pages/posts dimension including type and category metadata
view: wordpress_posts {
derived_table: {
sql: SELECT * FROM
(
SELECT
p.post_title AS Post_Title,
'Content' AS post_type,
t.name AS Category,
p.post_date AS Post_Date,
p.post_name AS Post_Name,
@markrittman
markrittman / scorecard.view.lkml
Created July 18, 2022 21:25
Example balanced scorecard calculation for a Looker dashboard
view: kpi_scorecard {
derived_table: {
sql: with delivery_kpi_1 as (
(
SELECT
timestamp(date_trunc(contact_utilization_fact.forecast_week,MONTH)) as kpi_month,
'Utilization' as kpi_name,
max(0.5) as category_weighting_pct,
COALESCE(SUM(contact_utilization_fact.actual_billable_hours ),
0) / COALESCE(SUM(contact_utilization_fact.target_billable_capacity ),
@markrittman
markrittman / ga4_session_event_counts.sql
Last active June 7, 2022 03:34
Count various event types for a GA4 session, whether user is returning and whether it's a bounced session and/or converting session
WITH events as (SELECT
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_number') AS session_number,
replace(lower(event_name),' ','_') event_name,
count(*) events
FROM `ra-development.analytics_277223877.events_*`
GROUP BY
1, 2, 3, 4),
session_events_pivoted as (
@markrittman
markrittman / segment_consideration_stage_journey.sql
Created May 29, 2022 13:28
Segment Events unioned with Identify Event, ID Stitched, User Details Backfilled and Events Grouped into User Consideration-Stage Journey
with events as (
select
anonymous_id,
user_id,
cast(null as string) as name,
cast(null as string) as email,
timestamp,
'page_view' as event_type,
concat(split(context_ip,'.')[safe_offset(0)],'.***.***.',split(context_ip,'.')[safe_offset(3)]) as context_ip,
context_page_path,
@markrittman
markrittman / segment_inferred_identity_stitching.sql
Created May 23, 2022 20:50
Example BigQuery SQL statement to stitch user identities together across multiple Segment and other sources based on inferred connections
with
meeting_bookings as # meetings have names and email addresses, but no anonymous_id
(
select
# union of three events that are logically the same but were recorded as three different events historically
id,
timestamp,
email as anonymous_id,
cast(null as string) as context_ip,
cast(null as string) as context_page_path,
@markrittman
markrittman / snowflake_dbt_setup.sql
Created March 2, 2022 17:24
Setup Script for Snowflake DW for use with dbt
use role accountadmin;
create or replace role loader;
create or replace role transformer;
create or replace role reporter;
create or replace role looker_role;
grant role loader to role sysadmin;
grant role transformer to role sysadmin;
grant role reporter to role sysadmin;
grant role looker_role to role sysadmin;