<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 関聖矢</title>
    <description>The latest articles on DEV Community by 関聖矢 (@logicalweb).</description>
    <link>https://dev.to/logicalweb</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4260836%2Fea1a2803-e95a-4bc9-a8b0-c4185930c4b9.jpg</url>
      <title>DEV Community: 関聖矢</title>
      <link>https://dev.to/logicalweb</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/logicalweb"/>
    <language>en</language>
    <item>
      <title>GA4 BigQuery UNNEST: How to Extract event_params Without the NULLs and Errors</title>
      <dc:creator>関聖矢</dc:creator>
      <pubDate>Mon, 23 Nov 2026 14:41:57 +0000</pubDate>
      <link>https://dev.to/logicalweb/ga4-bigquery-unnest-how-to-extract-eventparams-without-the-nulls-and-errors-2k4k</link>
      <guid>https://dev.to/logicalweb/ga4-bigquery-unnest-how-to-extract-eventparams-without-the-nulls-and-errors-2k4k</guid>
      <description>&lt;h1&gt;
  
  
  GA4 BigQuery UNNEST: How to Extract event_params Without the NULLs and Errors
&lt;/h1&gt;

&lt;p&gt;If you have exported GA4 to BigQuery, you have probably hit this moment: you open the &lt;code&gt;events_*&lt;/code&gt; table, find the column that should hold the page URL or the session ID… and it's not a column at all. It's a nested, repeated record called &lt;code&gt;event_params&lt;/code&gt;, and almost everything you actually want to analyse is buried inside it.&lt;/p&gt;

&lt;p&gt;This is the single biggest reason people stall with the GA4 BigQuery export. The data is all there — page locations, session IDs, engagement time, campaign values, your custom parameters — but none of it is directly queryable until you UNNEST it correctly.&lt;/p&gt;

&lt;p&gt;This guide covers the patterns I use in client work, the three mistakes that produce errors or silent NULLs, and how to stop repeating the same extraction code in every query.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why event_params is nested in the first place
&lt;/h2&gt;

&lt;p&gt;GA4 events are flexible by design. A &lt;code&gt;page_view&lt;/code&gt; carries &lt;code&gt;page_location&lt;/code&gt; and &lt;code&gt;page_title&lt;/code&gt;; a &lt;code&gt;purchase&lt;/code&gt; carries transaction values; your app may send dozens of custom parameters that another site never sends. Instead of a fixed column per parameter, BigQuery stores them as an array of key/value pairs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;key&lt;/code&gt; — the parameter name, e.g. &lt;code&gt;page_location&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value.string_value&lt;/code&gt;, &lt;code&gt;value.int_value&lt;/code&gt;, &lt;code&gt;value.float_value&lt;/code&gt;, &lt;code&gt;value.double_value&lt;/code&gt; — the value, stored in exactly one of these typed fields depending on its type&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That design is why a flat &lt;code&gt;SELECT event_params.page_location&lt;/code&gt; cannot work, and why the extraction pattern below becomes the foundation of nearly every GA4 query you will ever write.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 1: Extract one or more parameters with a subquery
&lt;/h2&gt;

&lt;p&gt;The most readable and most reusable approach is a scalar subquery per parameter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="n"&gt;event_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_pseudo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ga_session_id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;ga_session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'page_location'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;page_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'page_title'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;page_title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'page_referrer'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;page_referrer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'engagement_time_msec'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;engagement_time_msec&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="sb"&gt;`{{project_id}}.{{dataset}}.events_*`&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;_TABLE_SUFFIX&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="s1"&gt;'{{start_date}}'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="s1"&gt;'{{end_date}}'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things to notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each parameter is pulled out with its &lt;strong&gt;own typed field&lt;/strong&gt;. &lt;code&gt;ga_session_id&lt;/code&gt; is an integer, so it comes from &lt;code&gt;value.int_value&lt;/code&gt;. &lt;code&gt;page_location&lt;/code&gt; is a string, so it comes from &lt;code&gt;value.string_value&lt;/code&gt;. Get the type wrong and you don't get an error — you get NULL, silently, on every row (more on that below).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;WHERE _TABLE_SUFFIX BETWEEN ...&lt;/code&gt; clause is not optional decoration. Without it, BigQuery scans every daily table in the dataset. On a property with a year of history, that is the difference between a cheap query and an expensive surprise.&lt;/li&gt;
&lt;li&gt;This pattern scales fine. Five parameters, ten parameters — the query stays readable, and each line can be copied into other queries as-is.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One nuance: if the same key somehow appears twice in one event, this subquery form will fail with a "scalar subquery produced more than one element" error. In standard GA4 exports that is rare, but if you hit it, add &lt;code&gt;LIMIT 1&lt;/code&gt; inside the subquery while you investigate your tagging.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 2: Build a proper session ID (and why ga_session_id alone is wrong)
&lt;/h2&gt;

&lt;p&gt;Once you can extract &lt;code&gt;ga_session_id&lt;/code&gt;, the next trap is using it on its own. Don't. &lt;code&gt;ga_session_id&lt;/code&gt; is a timestamp-based value that is &lt;strong&gt;only unique within a user&lt;/strong&gt;. Two different users can absolutely share the same &lt;code&gt;ga_session_id&lt;/code&gt;, so counting distinct &lt;code&gt;ga_session_id&lt;/code&gt; values undercounts sessions and joining on it creates phantom matches.&lt;/p&gt;

&lt;p&gt;The fix is to combine it with &lt;code&gt;user_pseudo_id&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="n"&gt;event_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;DISTINCT&lt;/span&gt;
    &lt;span class="nb"&gt;CONCAT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="n"&gt;user_pseudo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nb"&gt;CAST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int_value&lt;/span&gt;
         &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
         &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ga_session_id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;STRING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;session_count&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="sb"&gt;`{{project_id}}.{{dataset}}.events_*`&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;_TABLE_SUFFIX&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="s1"&gt;'{{start_date}}'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="s1"&gt;'{{end_date}}'&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;event_date&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;event_date&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that definition, a daily session count becomes a straightforward &lt;code&gt;COUNT(DISTINCT ...)&lt;/code&gt; grouped by &lt;code&gt;event_date&lt;/code&gt;, and it reconciles far better against the GA4 interface. (It will rarely match the UI &lt;em&gt;exactly&lt;/em&gt; — the GA4 UI applies its own processing and, for some reports, estimation. A small, explainable gap is normal; a large one usually means the session definition, not the data, is wrong.)&lt;/p&gt;

&lt;p&gt;If your site issues a signed-in &lt;code&gt;user_id&lt;/code&gt;, consider building the session ID from that instead of &lt;code&gt;user_pseudo_id&lt;/code&gt; — it stitches sessions across devices, which cookie-based IDs cannot do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pattern 3: UNNEST the items array for product-level data
&lt;/h2&gt;

&lt;p&gt;E-commerce data lives in a different nested field: &lt;code&gt;items&lt;/code&gt;. Unlike &lt;code&gt;event_params&lt;/code&gt;, you usually want one row &lt;em&gt;per item&lt;/em&gt;, so instead of a subquery you join the unnested array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="n"&gt;event_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_pseudo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;item_category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;quantity&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="sb"&gt;`{{project_id}}.{{dataset}}.events_*`&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;_TABLE_SUFFIX&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="s1"&gt;'{{start_date}}'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="s1"&gt;'{{end_date}}'&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;event_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'purchase'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two gotchas here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;CROSS JOIN UNNEST&lt;/code&gt; drops events with no items.&lt;/strong&gt; For &lt;code&gt;purchase&lt;/code&gt; or &lt;code&gt;view_item&lt;/code&gt; that is normally what you want. If you are analysing an event type where some rows have items and some don't, and the item-less rows still matter, switch to &lt;code&gt;LEFT JOIN UNNEST(items) AS item&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never write &lt;code&gt;items.item_name&lt;/code&gt; directly.&lt;/strong&gt; &lt;code&gt;items&lt;/code&gt; is an array; referencing a field on it without UNNESTing produces the classic error: &lt;code&gt;Cannot access field item_name on a value with type ARRAY&amp;lt;...&amp;gt;&lt;/code&gt;. That error message is practically a rite of passage — if you've seen it, this pattern is the fix.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The same subquery approach from Pattern 1 also works for &lt;code&gt;user_properties&lt;/code&gt; — for example pulling &lt;code&gt;first_open_time&lt;/code&gt; or your own custom user properties such as a membership tier — just UNNEST &lt;code&gt;user_properties&lt;/code&gt; instead of &lt;code&gt;event_params&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The 3 mistakes that waste the most time
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Wrong key name.&lt;/strong&gt; The session ID parameter is &lt;code&gt;ga_session_id&lt;/code&gt;, not &lt;code&gt;session_id&lt;/code&gt;. Custom parameters are worse: the key must match exactly what your implementation sends. When in doubt, query the distinct keys present in your own export before writing analysis SQL — guessing key names is how you get a full column of NULLs that &lt;em&gt;looks&lt;/em&gt; like "no data."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Wrong value type.&lt;/strong&gt; Every parameter value lives in exactly one typed field. Read an integer parameter via &lt;code&gt;value.string_value&lt;/code&gt; and you get NULL, not an error, which makes this the most dangerous mistake of the three — your query "works" and your report is wrong. Check the schema tab in the BigQuery console, or run a key inventory that counts how often each key appears with each value type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Forgetting the date filter.&lt;/strong&gt; A missing &lt;code&gt;_TABLE_SUFFIX&lt;/code&gt; condition means a full-dataset scan: slower, more expensive, and occasionally &lt;em&gt;wrong&lt;/em&gt;, because intraday (streaming) tables can be included in the wildcard depending on how you reference the tables. Note that intraday data is provisional — values can change or be replaced — so keep it out of anything you report as final. Always check the "bytes processed" estimate in the console before running a new query over a long period.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stop copy-pasting: turn the patterns into a view
&lt;/h2&gt;

&lt;p&gt;If you find yourself pasting the same five subqueries into every analysis — and you will — promote them into a staging view once, and write short queries against the view from then on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="k"&gt;REPLACE&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="sb"&gt;`{{project_id}}.{{staging_dataset}}.stg_events`&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="no"&gt;PARSE_DATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'%Y%m%d'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;event_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;event_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;user_pseudo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;CONCAT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;user_pseudo_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;CAST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'ga_session_id'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="no"&gt;STRING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'page_location'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;page_location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'page_title'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;page_title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;int_value&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nb"&gt;UNNEST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'engagement_time_msec'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;engagement_time_msec&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;CASE&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;event_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'session_start'&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="n"&gt;collected_traffic_source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manual_source&lt;/span&gt; &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;session_source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;CASE&lt;/span&gt; &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;event_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'session_start'&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="n"&gt;collected_traffic_source&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manual_medium&lt;/span&gt; &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;session_medium&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;device_category&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;geo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="sb"&gt;`{{project_id}}.{{dataset}}.events_*`&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;_TABLE_SUFFIX&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="s1"&gt;'{{start_date}}'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="s1"&gt;'{{end_date}}'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two things in this view deserve a second look:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The &lt;code&gt;_TABLE_SUFFIX&lt;/code&gt; filter is doing real work.&lt;/strong&gt; A view over &lt;code&gt;events_*&lt;/code&gt; with no date filter scans every daily table in the dataset every time anyone queries it — the full-dataset scan from Mistake #3 above, now baked into every query you write against the view. The placeholders above keep the same replace-three-values convention as the other patterns; if you create the view once for repeated use, swap them for a rolling window instead (for example, &lt;code&gt;_TABLE_SUFFIX&lt;/code&gt; between &lt;code&gt;FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY))&lt;/code&gt; and yesterday's date) so the scan stays bounded. Either way, always check the "bytes processed" estimate before running a new query against the view over a long period.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Source/medium is read from the &lt;code&gt;session_start&lt;/code&gt; event only.&lt;/strong&gt; &lt;code&gt;collected_traffic_source&lt;/code&gt; in the GA4 export is an event-level struct, but the values that describe the &lt;em&gt;session's&lt;/em&gt; traffic source are the ones carried on the session's &lt;code&gt;session_start&lt;/code&gt; event — taking the struct from whichever event happens to be on the row is how attribution quietly diverges between reports. The &lt;code&gt;CASE&lt;/code&gt; expressions above therefore return &lt;code&gt;session_source&lt;/code&gt; / &lt;code&gt;session_medium&lt;/code&gt; only on the &lt;code&gt;session_start&lt;/code&gt; row and NULL on every other row, by design. For session-level reporting, aggregate them per session (&lt;code&gt;MAX(session_source)&lt;/code&gt;, &lt;code&gt;MAX(session_medium)&lt;/code&gt; grouped by &lt;code&gt;session_id&lt;/code&gt;); don't expect them to be populated event by event.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With the view in place, most day-to-day questions — sessions by channel, pageviews per session, engagement by device — become five-line queries instead of fifty-line ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get the free sample pack
&lt;/h2&gt;

&lt;p&gt;These patterns are templates 01 and 02 from a pack of 20 production GA4 × BigQuery SQL templates I use and maintain in client work — covering the basics above plus e-commerce funnels, channel CAC, LTV and cohort analysis, Search Console joins, and query-cost monitoring. Every template uses the same placeholders (&lt;code&gt;{{project_id}}&lt;/code&gt;, &lt;code&gt;{{dataset}}&lt;/code&gt;, date range), so you replace three values and run it.&lt;/p&gt;

&lt;p&gt;You can download a &lt;strong&gt;free sample with 3 full templates&lt;/strong&gt; (UNNEST patterns, session definition, and the e-commerce funnel) here: &lt;a href="https://logicalweb.gumroad.com/l/qgozm" rel="noopener noreferrer"&gt;https://logicalweb.gumroad.com/l/qgozm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the sample saves you time, the full pack of 20 is available here: &lt;a href="https://logicalweb.gumroad.com/l/pksizb" rel="noopener noreferrer"&gt;https://logicalweb.gumroad.com/l/pksizb&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;— I run a GA4 × BigQuery consultancy in Japan (logical-web.jp) and write about this export for a living. If a query above doesn't reconcile with your data, the session definition and the intraday tables are the first two places to look.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>bigquery</category>
      <category>ga4</category>
      <category>sql</category>
      <category>analytics</category>
    </item>
  </channel>
</rss>
