189 애트리뷰션 개념을 콘텐츠 분석에 적용하기
Google 애널리틱스 4의 데모 계정에서 “purchase” 이벤트를 전환으로 간주하여, 페이지별 전환 기여도를 애트리뷰션 모델별로 구하는 SQL 문입니다.
WITH
cv_users
AS
(
SELECT
user_pseudo_id
,
MAX
(IF(event_name =
'purchase'
, 1, 0))
AS
cv_flag
,
MIN
(IF(event_name =
'purchase'
, event_timestamp,
NULL
))
AS
min_cv_timestamp
FROM
`bigquery-
public
-data.ga4_obfuscated_sample_ecommerce.events_202101*`
GROUP
BY
user_pseudo_id
HAVING
cv_flag = 1
)
, pageviews
AS
(
SELECT
*
FROM
(
SELECT
user_pseudo_id, event_name
, event_timestamp
, (
SELECT
value.string_value
FROM
UNNEST(event_params)
WHERE
key
=
'page_title'
)
AS
page_title
FROM
`bigquery-
public
-data.ga4_obfuscated_sample_ecommerce.events_202101*`
WHERE
event_name
IN
(
'page_view'
,
'purchase'
)
)
WHERE
page_title
NOT
IN
(
'Shopping Cart'
,
'Checkout Your Information'
,
'Payment Method'
,
'Checkout Confirmation'
,
'Checkout Review'
)
)
, master
AS
(
SELECT
*
FROM
(
SELECT
pv.user_pseudo_id
, pv.event_name
, pv.page_title
, pv.event_timestamp
, cv.min_cv_timestamp
, ROW_NUMBER() OVER(PARTITION
BY
pv.user_pseudo_id
ORDER
BY
pv.event_timestamp)
AS
row_num
FROM
pageviews
AS
pv
INNER
JOIN
cv_users
AS
cv
ON
pv.user_pseudo_id = cv.user_pseudo_id
)
WHERE
event_timestamp < min_cv_timestamp
)
SELECT
page_title
,
SUM
(first_touch)
AS
first_touch
,
SUM
(last_touch)
AS
last_touch
,
SUM
(linear)
AS
linear
,
SUM
(touch_point)
AS
touch_point
FROM
(
SELECT
*
, IF(row_num =
MIN
(row_num) OVER(PARTITION
BY
user_pseudo_id), 1, 0)
AS
first_touch
, IF(row_num =
MAX
(row_num) OVER(PARTITION
BY
user_pseudo_id), 1, 0)
AS
last_touch
, 1 /
MAX
(row_num) OVER(PARTITION
BY
user_pseudo_id)
AS
linear
,
CASE
WHEN
MAX
(row_num) OVER(PARTITION
BY
user_pseudo_id) = 1
THEN
1
WHEN
MAX
(row_num) OVER(PARTITION
BY
user_pseudo_id) = 2
THEN
0.5
ELSE
CASE
WHEN
row_num =
MIN
(row_num) OVER(PARTITION
BY
user_pseudo_id)
THEN
0.4
WHEN
row_num =
MAX
(row_num) OVER(PARTITION
BY
user_pseudo_id)
THEN
0.4
ELSE
0.2 / (
MAX
(row_num) OVER(PARTITION
BY
user_pseudo_id) - 2)
END
END
AS
touch_point
FROM
master
)
GROUP
BY
page_title
ORDER
BY
2
DESC
답글 남기기