Соедините уникальные идентификаторы пользователей с первыми идентификаторами пользователей предыдущего месяца - PullRequest
0 голосов
/ 10 июля 2019

Я хочу идентифицировать пользователей, которые имели событие first_open в месяце a (здесь: январь) и вернулись к нашему в месяце b (здесь: февраль) с событием user_engagement.

Моя идея: 1. Создайте таблицу со всеми пользователями, у которых было событие first_open 2. Создайте таблицу со всеми пользователями, у которых было событие user_engagement 3. Объедините обе таблицы по идентификатору пользователя. 4. Подсчитайте пользователей, у которых было событие first_open в месяце a и в месяце b, и подсчитайте всех пользователей за январь с событием first_open

С помощью следующего запроса в настоящее время я перебиваю обоих пользователей за месяц a и b, потому что я не учитываю всех неисчислимых пользователей для обоих типов событий.


    With
    users_first_open as (select 
    user_pseudo_id,
    EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS install_month,
    event_name as firstopen
    FROM
        `table.events_*`
    where _TABLE_SUFFIX BETWEEN '20190101'
        AND '20190108' and event_name = "first_open" and 
        EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 1
    ),

    user_enagement_next_month as (select 
    user_pseudo_id,
    EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS engagement_month,
    event_name as engagament_next_month
    FROM
        `table.events_*`
    where _TABLE_SUFFIX BETWEEN '20190109'
        AND '20190116' and event_name = "user_engagement"
        and EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 1), 

    cohort_raw as(
    select 
    user_pseudo_id,
    install_month,
    engagement_month, 
    case when firstopen = "first_open" then 1 else 0 end as cohort_count_first_open, 
    case when engagament_next_month = "user_engagement" then 1 else 0 end as cohort_count_engagement
    from 
    user_enagement_next_month
    full join 
    users_first_open using (user_pseudo_id))--, 


    select
    sum(case when cohort_count_first_open is not null then 1 else 0 end) as users_first_open,
    (select sum(case when cohort_count_engagement is not null then 1 else 0 end) as u_engagement_open from cohort_raw where cohort_count_first_open = 1) as users_engagement_open
    from cohort_raw

Далее я попробовал следующее: группа в таблице 2 "user_enagement_next_month" по идентификатору пользователя и т. Д. и создать сумму "first_open", когда дело и "привлечение", когда результаты. С последующим я включил запрос только для подсчета пользователей, чье количество этих двух было = 2


    With
    users_first_open as (select 
    user_pseudo_id,
    EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS install_month,
    event_name as firstopen
    FROM
        `table.events_*`
    where _TABLE_SUFFIX BETWEEN '20190101'
        AND '20190131' and event_name = "first_open" and 
        EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 1
    ),

    user_enagement_next_month as (select 
    user_pseudo_id,
    EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) AS engagement_month,
    event_name as engagament_next_month
    FROM
        `table.events_*`
    where _TABLE_SUFFIX BETWEEN '20190201'
        AND '20190228' and event_name = "session_start"
        and EXTRACT (Month FROM(DATE(TIMESTAMP_MICROS(user_first_touch_timestamp)))) = 2
        group by 1,2,3)--,

    --cohort_raw as(
    select 
    user_pseudo_id,
    install_month,
    engagement_month, 
    case when firstopen = "first_open" then 1 else 0 end as cohort_count_first_open, 
    case when engagament_next_month = "session_start" then 1 else 0 end as cohort_count_engagement
    --case when user_pseudo_id is not null then 1 else 0 end as cohort_count_engagement
    from 
    user_enagement_next_month 
    full join 
    users_first_open using (user_pseudo_id)), 

    cohort_agg as (
    select *, cohort_count_first_open+cohort_count_engagement as cohort_sum
    from cohort_raw
    group by 1,2,3,4,5
    order by 6 desc)

    select
    (select count(*) from users_first_open) as cohort_jan,
    (select Sum(cohort_sum) from cohort_agg where cohort_sum = 2) as ret, 
    sum(case when cohort_count_first_open is not null then 1 else 0 end) as users_first_open,
    (select sum(case when cohort_count_engagement is not null then 1 else 0 end) as u_engagement_open from cohort_raw where cohort_count_first_open = 1) as users_engagement_open
    from cohort_agg 

Я ожидаю доходность около 20%. Мой вывод на данный момент составляет 54%, потому что в моем запросе я либо переоцениваю, либо считаю слишком мало, потому что я предполагаю, что мое объединение не работает.

1 Ответ

0 голосов
/ 28 июля 2019

Может быть, я не совсем понимаю, что вы хотите, но попробуйте это

with

users_first_open as (
    select distinct  -- is there duplicates for one user_id?
        user_pseudo_id,

        extract(
            month from
            timestamp_micros(user_first_touch_timestamp)
        ) as install_month
    from
        `table.events_201901*`  -- longer prefixes generally perform better
    where
        _table_suffix between '01' and '31'
        and event_name = 'first_open'
        and extract(
                month from
                timestamp_micros(user_first_touch_timestamp)
            ) = 1
),

user_enagement_next_month as (
    select distinct
        user_pseudo_id,

        extract(
            month from
            timestamp_micros(user_first_touch_timestamp)
        ) as engagement_month
    from
        `table.events_201902*`  -- longer prefixes generally perform better
    where
        _table_suffix between '01' and '28'
        and event_name = 'user_engagement'
        and extract(
                month from
                timestamp_micros(user_first_touch_timestamp)
            ) = 2
)

select 
    ufo.install_month,
    uenm.engagement_month,
    count(*) as first_open_event_users_cnt,
    count(uenm.user_pseudo_id) as user_engagement_event_users_cnt
from 
    users_first_open as ufo
    left join user_enagement_next_month as uenm
        on ufo.user_pseudo_id = uenm.user_pseudo_id
group by
    1, 2

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...