Skip to content

Instantly share code, notes, and snippets.

View getconfection's full-sized avatar

Confection getconfection

View GitHub Profile
@getconfection
getconfection / detailed-event-info.txt
Last active September 4, 2024 23:26
"I want to see detailed information about an event."
/* Use this query to answer the following questions: On which page did the event fire? How many times did it fire? What values are available? Where did the user who triggered the event come from? */
DECLARE __date_init DATE;
DECLARE __date_end DATE ;
DECLARE __event_name STRING ;
DECLARE __domain STRING ;
/* Define the date range, event name, and relevant doamin(s). */
SET __date_init = '2023-09-01'; /* Return results that happened on this date or after ... */
SET __date_end = '2023-09-30'; /* .. and on this date or after. */
SET __event_name = 'eventName'; /* Replace eventName with your custom event name. You can also use Confection default events like loadtime, pageviewBatch, and purchasedEvent. */
@getconfection
getconfection / Isolate Anonymous LinkedIn IDs
Last active August 28, 2024 19:12
Isolate Anonymous LinkedIn IDs
WITH raw_data AS (
SELECT
TO_JSON_STRING(JSON_EXTRACT(data, '$')) AS json_string
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
WHERE JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'domain2.com', 'domain3.com') /* To return results from specific domains, enter one or more names here. */
AND TIMESTAMP_SECONDS(CAST(JSON_EXTRACT_SCALAR(data, '$.created') AS INT64))
BETWEEN TIMESTAMP('2024-08-01') AND TIMESTAMP('2024-08-31') /* Set your timeframe here. */
)
SELECT
-- Get Cookie Values
@getconfection
getconfection / Isolate Anonymous HubSpot IDs
Last active September 25, 2023 20:18
Isolate Anonymous HubSpot IDs
SELECT
JSON_EXTRACT_SCALAR(parsed.value, '$.hubspotutk') AS hubspotutk,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_acc') AS hsa_acc,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_cam') AS hsa_cam,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_grp') AS hsa_grp,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_ad') AS hsa_ad,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_src') AS hsa_src,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_tgt') AS hsa_tgt,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_kw') AS hsa_kw,
JSON_EXTRACT_SCALAR(parsed.value, '$.hsa_mt') AS hsa_mt,
@getconfection
getconfection / Isolate Anonymous Google IDs
Last active August 28, 2024 19:12
Isolate Anonymous Google IDs
WITH raw_data AS (
SELECT
TO_JSON_STRING(JSON_EXTRACT(data, '$')) AS json_string
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
WHERE JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'domain2.com', 'domain3.com')
AND TIMESTAMP_SECONDS(CAST(JSON_EXTRACT_SCALAR(data, '$.created') AS INT64))
BETWEEN TIMESTAMP('2024-08-01') AND TIMESTAMP('2024-08-31') /* Set your timeframe here. */
)
SELECT
-- Get Cookie Values
@getconfection
getconfection / Isolate Anonymous Facebook IDs
Last active August 28, 2024 19:12
Isolate Anonymous Facebook IDs
WITH raw_data AS (
SELECT
TO_JSON_STRING(JSON_EXTRACT(data, '$')) AS json_string
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
WHERE JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'domain2.com', 'domain3.com') /* To return results from specific domains, enter one or more names here. */
AND TIMESTAMP_SECONDS(CAST(JSON_EXTRACT_SCALAR(data, '$.created') AS INT64))
BETWEEN TIMESTAMP('2024-08-01') AND TIMESTAMP('2024-08-31') /* Set your timeframe here. */
)
SELECT
-- Get Cookie Values
@getconfection
getconfection / conversion-rate-event-UUID
Last active August 21, 2023 18:41
"I want to see an event's conversion rate vs. number of visitors/users (unique UUIDs)."
/* We're using the following formula to determine a conversion rate: (total times an event fired / total number of UUIDs)*100 */
WITH EventFirings AS (
SELECT
COUNT(*) AS total_firings
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
WHERE
JSON_EXTRACT_SCALAR(data, '$.events.eventName.value') IS NOT NULL /* Replace eventName with your event name. */
AND JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'domain2.com', 'domain3.com') /* To return results from specific domains, enter one or more names here. */
AND TIMESTAMP_SECONDS(CAST(JSON_EXTRACT_SCALAR(data, '$.events.eventName.created_time') AS INT64)) BETWEEN TIMESTAMP('2023-08-01') AND TIMESTAMP('2023-08-31') /* Set your date range here. */
),
@getconfection
getconfection / first-touch.txt
Last active December 11, 2023 19:19
"I want to see a list of email addresses and their first-touch source data."
SELECT DISTINCT
email_address AS Email,
JSON_EXTRACT_SCALAR(data, '$.referrer') AS First_Referrer,
JSON_EXTRACT_SCALAR(data, '$.utm_campaign') AS First_UTM_Campaign,
JSON_EXTRACT_SCALAR(data, '$.utm_content') AS First_UTM_Content,
JSON_EXTRACT_SCALAR(data, '$.utm_id') AS First_UTM_ID,
JSON_EXTRACT_SCALAR(data, '$.utm_medium') AS First_UTM_Medium,
JSON_EXTRACT_SCALAR(data, '$.utm_source') AS First_UTM_Source,
JSON_EXTRACT_SCALAR(data, '$.utm_term') AS First_UTM_Term
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
@getconfection
getconfection / event-value-count.txt
Created July 25, 2023 17:43
"I want to see how many times individual event values have fired."
WITH formtype_data AS (
SELECT
JSON_EXTRACT_SCALAR(data, '$.events.eventName.created_time') AS formType_created_time, /* Replace eventName with your event name. */
JSON_EXTRACT_SCALAR(data, '$.events.eventName.value') AS formType_value /* Replace eventName with your event name. */
FROM
`projectname.confection.leads` /* Replace projectname with your project name. */
WHERE
JSON_EXTRACT_SCALAR(data, '$.events.eventName.value') IN ('value1', 'value2') /* Replace eventName with your event name. Replace value1 and value2 with event values. Add others (comma separated) as necessary. */
AND JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'domain2.com', 'domain3.com') /* To return results from specific domains, enter one or more names here. */
)
@getconfection
getconfection / emails-urls-pageviews.txt
Last active December 12, 2023 02:00
"I want to generate a list of emails, the URLs the user visited, and the number of times the user visited those URLs."
SELECT
JSON_EXTRACT_SCALAR(data, '$.url') AS url,
COUNT(*) AS count,
email_address AS email
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
CROSS JOIN UNNEST(REGEXP_EXTRACT_ALL(TO_JSON_STRING(data), r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}')) AS email_address
WHERE JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'www.domain2.com', 'domain3.com') /* To return results from specific domains, enter one or more names here. Be sure to add www if your domain(s) include it. */
AND email_address NOT IN ('excluded1@example.com', 'excluded2@example.com', 'excluded3@example.com') /* Add any emails you want to exclude. */
AND TIMESTAMP_SECONDS(CAST(JSON_EXTRACT_SCALAR(data, '$.created') AS INT64)) BETWEEN TIMESTAMP('2020-09-01') AND TIMESTAMP('2023-09-30') /* Set your timeframe here. */
GROUP BY url, email;
@getconfection
getconfection / urls-and-pageviews.txt
Last active November 6, 2024 18:01
"I want to see a list of URLs and their associated pageview counts."
SELECT JSON_EXTRACT_SCALAR(data, '$.url') AS URL,
COUNT(*) AS Pageviews
FROM `projectname.confection.leads` /* Replace projectname with your project name. */
WHERE JSON_EXTRACT_SCALAR(data, '$.domain') IN ('domain1.com', 'domain2.com', 'domain3.com') /* To return results from specific domains, enter one or more names here. */
AND TIMESTAMP_SECONDS(CAST(JSON_EXTRACT_SCALAR(data, '$.created') AS INT64)) BETWEEN TIMESTAMP('2024-10-28') AND TIMESTAMP('2024-11-03') /* Replace with desired date range */
GROUP BY URL;