Advertisement
kirzecy670

Untitled

Aug 25th, 2024 (edited)
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.72 KB | None | 0 0
  1. WITH
  2.     -- Extract relevant fields from JSON
  3.     visitParamExtractString(addJson, 'option') AS option_,
  4.     visitParamExtractString(addJson, 'id') AS event_id_,
  5.     visitParamExtractInt(addJson, 'type') AS push_id_,
  6.     visitParamExtractString(addJson, 'event') AS event_name_,
  7.  
  8. -- Identify experiment groups
  9. experiment_groups AS (
  10.     SELECT
  11.         uid,
  12.         MIN(option_) AS experiment_group_id
  13.     FROM
  14.         stat.funnelTrack
  15.     WHERE
  16.         dt >= '2024-08-13'
  17.         AND action = 'pmr_2020_highlights_experiment'
  18.     GROUP BY
  19.         uid
  20. ),
  21.  
  22. -- Collect sent pushes for the 'old' experiment group
  23. sent_pushes_old AS (
  24.     SELECT
  25.         eg.experiment_group_id as experiment_group_id,
  26.         ft.dt as dt,
  27.         ft.uid as uid,
  28.         push_id_ as push_id,
  29.         event_id_ as event_id
  30.     FROM
  31.         stat.funnelTrack ft
  32.     JOIN
  33.         experiment_groups eg USING(uid)
  34.     WHERE
  35.         action IN ('cdp_push', 'admin_push', 'custom_push')
  36.         AND dt >= '2024-08-13'
  37.         AND push_id IN (520, 523, 524, 525, 526, 527, 530)
  38.         AND experiment_group_id = 'old'
  39. ),
  40.  
  41. -- Collect sent pushes for the 'new' experiment group
  42. sent_pushes_new AS (
  43.     SELECT
  44.         eg.experiment_group_id as experiment_group_id,
  45.         ps.dt as dt,
  46.         ps.user_uid AS uid,
  47.         push_id
  48.     FROM
  49.         analytics.pushes_sent ps
  50.     JOIN
  51.         experiment_groups eg ON eg.uid = ps.user_uid
  52.     WHERE
  53.         dt >= '2024-08-13'
  54.         AND sub_type = 'UP'
  55.         AND type IN ('2hr_video', 'week_app', '2hr_games', '2hr_social', 'night_activity')
  56.         AND eg.experiment_group_id = 'new'
  57. ),
  58.  
  59. -- Combine push IDs for matching against opened pushes
  60. combined_push_ids AS (
  61.     SELECT DISTINCT uid, event_id FROM (
  62.         SELECT uid, event_id FROM sent_pushes_old
  63.         UNION ALL
  64.         SELECT uid, push_id AS event_id FROM sent_pushes_new
  65.     )
  66. ),
  67.  
  68. -- Collect information about opened pushes
  69. opened_pushes AS (
  70.     SELECT
  71.         eg.experiment_group_id as experiment_group_id,
  72.         ft.uid as uid
  73.     FROM
  74.         stat.funnelTrack ft
  75.     JOIN
  76.         experiment_groups eg ON ft.uid = eg.uid
  77.     JOIN
  78.         combined_push_ids pi ON pi.uid = ft.uid AND pi.event_id = visitParamExtractString(addJson, 'event')
  79.     WHERE
  80.         action = 'push_open'
  81.         AND dt >= '2024-08-13'
  82. )
  83.  
  84. -- Final aggregation and reporting
  85. SELECT
  86.     eg.experiment_group_id as experiment_group_id,
  87.     eg.total_users as total_users,
  88.     -- COALESCE(sp_old.users_sent, sp_new.users_sent) AS users_sent,
  89.     CASE
  90.         WHEN experiment_group_id = 'old' THEN sp_old.users_sent
  91.         WHEN experiment_group_id = 'new' THEN sp_new.users_sent
  92.     ELSE null
  93.     END AS users_sent,
  94.     op.pushes_opened,
  95.     ROUND((sp_old.pushes_sent + sp_new.pushes_sent) / eg.total_users, 2) AS pushes_sent_per_user
  96. FROM
  97. -- Total users per experiment group
  98.     (SELECT experiment_group_id, uniqExact(uid) AS total_users FROM experiment_groups GROUP BY experiment_group_id) eg
  99.  
  100. -- Sent pushes for the 'old' group
  101. LEFT JOIN
  102.     (SELECT experiment_group_id, uniqExact(uid) AS users_sent, COUNT(*) AS pushes_sent FROM sent_pushes_old GROUP BY experiment_group_id) sp_old
  103. ON
  104.     sp_old.experiment_group_id = eg.experiment_group_id
  105.  
  106. -- Sent pushes for the 'new' group
  107. LEFT JOIN
  108.     (SELECT experiment_group_id, uniqExact(uid) AS users_sent, COUNT(*) AS pushes_sent FROM sent_pushes_new GROUP BY experiment_group_id) sp_new
  109. ON
  110.     sp_new.experiment_group_id = eg.experiment_group_id
  111.  
  112. -- Opened pushes for both groups
  113. LEFT JOIN
  114.     (SELECT experiment_group_id, uniqExact(uid) AS pushes_opened FROM opened_pushes GROUP BY experiment_group_id) op
  115. ON
  116.     op.experiment_group_id = eg.experiment_group_id
  117. ORDER BY 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement