简体   繁体   English

x 将值设置为两个变量时的 BigQuery 案例

[英]BigQuery case when x set values to two variables

I am trying to identify session-level traffic sources in GA4 data within BigQuery.我正在尝试在 BigQuery 中识别 GA4 数据中的会话级流量来源。 I initially used the traffic_source variables, until I realised they relate to the traffic source that first acquired the user: ie if I searched for Apple on Google then my traffic_source would be organic.我最初使用 traffic_source 变量,直到我意识到它们与首先获得用户的流量来源相关:即如果我在 Google 上搜索 Apple,那么我的 traffic_source 将是有机的。 if I then click on an email link from Apple my traffic_source remains organic (not email).如果我然后单击来自 Apple 的 email 链接,我的 traffic_source 仍然是有机的(不是电子邮件)。

I can spot traffic from marketing channels with utm tags from the session start event, but am now trying to find traffic from channels that don't use utm tags: direct, organic search and referral.我可以从 session 开始事件中使用 utm 标签发现来自营销渠道的流量,但我现在正试图从不使用 utm 标签的渠道中查找流量:直接、有机搜索和推荐。 If the page_location does not have utm tags and the event_params contains a populated field called referral, then I know where the traffic came from.如果 page_location 没有 utm 标签并且 event_params 包含一个名为 referral 的填充字段,那么我就知道流量来自哪里。

SELECT
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS url,
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer') AS referrer,
REPLACE(REGEXP_EXTRACT((SELECT value.string_value FROM UNNEST(event_params) WHERE 
 key = 'page_location'), 'utm_source=([^&]+)'), '%20', ' ') AS utm_source,
REPLACE(REGEXP_EXTRACT((SELECT value.string_value FROM UNNEST(event_params) WHERE 
 key = 'page_location'), 'utm_campaign=([^&]+)'), '%20', ' ') AS utm_campaign,
REPLACE(REGEXP_EXTRACT((SELECT value.string_value FROM UNNEST(event_params) WHERE 
 key = 'page_location'), 'utm_content=([^&]+)'), '%20', ' ') AS utm_content,
REPLACE(REGEXP_EXTRACT((SELECT value.string_value FROM UNNEST(event_params) WHERE 
 key = 'page_location'), 'utm_term=([^&]+)'), '%20', ' ') AS utm_term,
CASE
 WHEN 
REGEXP_CONTAINS((SELECT value.string_value FROM UNNEST(event_params) WHERE 
 key = 'page_location'), 'utm_') IS FALSE 
 AND (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer') IS NOT NULL 
      THEN 'organic' 
      AND utm_medium = (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_referrer')
      ELSE REPLACE(REGEXP_EXTRACT((SELECT value.string_value FROM UNNEST(event_params) WHERE 
      key = 'page_location'), 'utm_medium=([^&]+)'), '%20', ' ')
  END AS utm_medium
FROM `bigquery***.analytics_***.events_***`
WHERE event_name = 'session_start'

so in my query I am extracting the utm values from the page location and placing them in new columns.所以在我的查询中,我从页面位置提取 utm 值并将它们放在新列中。 I for organic search (which doesn't use utm tags) I am using a CASE statement.我用于自然搜索(不使用 utm 标签)我使用的是 CASE 语句。 I want to set utm_medium as 'organic' AND utm_source as the referral site.我想将 utm_medium 设置为“有机”并将 utm_source 设置为推荐站点。 Essentially trying to do this:本质上是尝试这样做:

CASE 
 WHEN A = 1 THEN
  B = 'apple' AND C = 'fruit'
END 

Is this possible in BigQuery?这在 BigQuery 中可能吗?

A CASE statement is an expression for creating a single column. CASE语句是用于创建单个列的表达式。 The simplest thing to do is to simply apply the same expression twice:最简单的做法是简单地应用相同的表达式两次:

...
    CASE WHEN a = 1 THEN 'apple' ELSE ... END AS b,
    CASE WHEN a = 1 THEN 'fruit' ELSE ... END AS c,
...

Alternatively, you could complicate things and use a STRUCT type column:或者,您可以使事情复杂化并使用STRUCT类型的列:

...
    CASE WHEN a = 1 THEN STRUCT('apple' AS B, 'fruit' AS C) ELSE ... END AS new_struct,
...

This accomplishes your goal in one step and allows you to do things like SELECT new_struct.B, new_struct.C in future.这一步完成了您的目标,并允许您将来执行SELECT new_struct.B, new_struct.C类的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM