Skip to content

Instantly share code, notes, and snippets.

nicobrx / adwords_report_scheduler.js
Created February 23, 2016 16:42
AdWords script - provides framework for scheduling reports in an MCC. Writes reports to a Google Sheet. Handles MCC limit of 50 accounts per script
* written by Nico Brooks -
* use at your own risk
* This script provides a generic framework for scheduling
* monthly AdWords reports and writing them to a Google sheet.
* To use, copy the script, set the global variables at the top of the file
* and customize the function getData. See
* for reporting options
* Writes a list of AdWords accounts in an MCC to a Google Sheet
* Useful in conjunction with scripts that run on batches of accounts
* The label-batching portion of this script was adapted from:
var TODAY_STR = Utilities.formatDate(new Date(), TIMEZONE, 'yyyy-MM');
* adapted from
* I include the GA object reference in my script, so that one version of
* the script works whether served in an iFrame or on a standalone page.
* I usually have to rely on the 3rd-party vendor to implement the code, so
* I like to keep implementation as simple as possible.
* adapted from
* I wrapped the whole thing in a setTimeout, so I could observe the delayed execution time
* of the GA function in the JavaScript console, using the GA debugger Chrome plugin.
* alternatively, you could just wrap the frameWindow.postMessage in a setTimeout
ga(function(tracker) {
if(!( && ga.create)) {
dataLayer.push({'event': 'gaNotLoaded'});
COUNT(DISTINCT(session_id)) AS sessions,
SUM(ga4_resource_page_views) AS ga4_resource_page_views
CONCAT(user_pseudo_id,(SELECT value.int_value FROM UNNEST(event_params) WHERE KEY = 'ga_session_id')) AS session_id,
COUNT((SELECT value.string_value FROM UNNEST(event_params) WHERE KEY = 'page_location'
AND value.string_value LIKE '')) AS ga4_resource_page_views,
PARSE_DATE('%Y%m%d',event_date) AS event_date
nicobrx / ga4_average_engagement_time_per_page.sql
Created June 8, 2023 04:09
Query for getting average engagement time per page from GA4 BigQuery data.
SUM(engagement_time_seconds) AS engagement_time_seconds,
SUM(page_views) AS page_views,
SAFE_DIVIDE( SUM(engagement_time_seconds), SUM(page_views) ) AS average_engagement_time_per_page
--Looker Studio BigQuery custom query to get sessions per user
with s as (select distinct
concat(user_pseudo_id,(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id')) AS session_id,
timestamp_micros(event_timestamp) as timestamp
from `yourbqproject.analytics_1234567.events_*`
where _table_suffix between @DS_START_DATE and @DS_END_DATE
and event_name = 'session_start')
-- day of week, starting with Sunday (dimension)
format_date('%w',parse_date("%Y%m%d",event_date)) as day_of_week,
-- hour (dimension)
format("%02d",extract(hour from timestamp_micros(event_timestamp))) as hour,
(select value.string_value from unnest(event_params) where key = 'page_location') as page_path,
count(event_name) as sessions
-- change this to your google analytics 4 export location in bigquery
nicobrx / ga4_page_value.sql
Last active June 1, 2024 10:03
Gets a page_value metric from GA4 data in BigQuery that is similar to the Universal Analytics Page Value metric: To use the query, replace the table references in lines 15 and 26 with your GA4 events table. Make sure to replace the table's date suffix with '*' so the query runs across date part…
-- set dates here so we don't have to update them in multiple places
with dates as (
'20230812' as start_date,
-- the next line gets yesterday
format_date('%Y%m%d',date_sub(current_date(), interval 1 day)) as end_date
-- get all events with event values
p as (