( websiteId: string, filters: QueryFilters, )
| 104 | } |
| 105 | |
| 106 | async function clickhouseQuery( |
| 107 | websiteId: string, |
| 108 | filters: QueryFilters, |
| 109 | ): Promise<{ x: string; y: number }[]> { |
| 110 | const { rawQuery, parseFilters } = clickhouse; |
| 111 | const { queryParams, filterQuery, cohortQuery, excludeBounceQuery, dateQuery } = parseFilters({ |
| 112 | ...filters, |
| 113 | websiteId, |
| 114 | }); |
| 115 | |
| 116 | const sql = ` |
| 117 | WITH channels as ( |
| 118 | select |
| 119 | session_id, |
| 120 | visit_id, |
| 121 | coalesce(nullIf(argMin(x, tuple(if(x != '', 0, 1), created_at, event_id)), ''), 'direct') as x |
| 122 | from ( |
| 123 | select |
| 124 | case when multiSearchAny(lower(utm_medium), ['cp', 'ppc', 'retargeting', 'paid']) != 0 then 'paid' else 'organic' end prefix, |
| 125 | case |
| 126 | when referrer_domain = '' and url_query = '' then 'direct' |
| 127 | when multiSearchAny(lower(url_query), [${toClickHouseStringArray( |
| 128 | PAID_AD_PARAMS, |
| 129 | )}]) != 0 then 'paidAds' |
| 130 | when multiSearchAny(lower(utm_medium), ['referral', 'app','link']) != 0 then 'referral' |
| 131 | when position(lower(utm_medium), 'affiliate') > 0 then 'affiliate' |
| 132 | when position(lower(utm_medium), 'sms') > 0 or position(lower(utm_source), 'sms') > 0 then 'sms' |
| 133 | when multiSearchAny(lower(referrer_domain), [${toClickHouseStringArray( |
| 134 | LLM_DOMAINS, |
| 135 | )}]) != 0 then 'llm' |
| 136 | when multiSearchAny(lower(referrer_domain), [${toClickHouseStringArray( |
| 137 | SEARCH_DOMAINS, |
| 138 | )}]) != 0 or position(lower(utm_medium), 'organic') > 0 then concat(prefix, 'Search') |
| 139 | when multiSearchAny(lower(referrer_domain), [${toClickHouseStringArray( |
| 140 | SOCIAL_DOMAINS, |
| 141 | )}]) != 0 then concat(prefix, 'Social') |
| 142 | when multiSearchAny(lower(referrer_domain), [${toClickHouseStringArray( |
| 143 | EMAIL_DOMAINS, |
| 144 | )}]) != 0 or position(lower(utm_medium), 'mail') > 0 then 'email' |
| 145 | when multiSearchAny(lower(referrer_domain), [${toClickHouseStringArray( |
| 146 | SHOPPING_DOMAINS, |
| 147 | )}]) != 0 or position(lower(utm_medium), 'shop') > 0 then concat(prefix, 'Shopping') |
| 148 | when multiSearchAny(lower(referrer_domain), [${toClickHouseStringArray( |
| 149 | VIDEO_DOMAINS, |
| 150 | )}]) != 0 or position(lower(utm_medium), 'video') > 0 then concat(prefix, 'Video') |
| 151 | when referrer_domain != hostname and referrer_domain != '' then 'referral' |
| 152 | else '' end AS x, |
| 153 | session_id, |
| 154 | visit_id, |
| 155 | event_id, |
| 156 | created_at |
| 157 | from website_event |
| 158 | ${cohortQuery} |
| 159 | ${excludeBounceQuery} |
| 160 | where website_id = {websiteId:UUID} |
| 161 | and event_type NOT IN (2, 5) |
| 162 | ${dateQuery} |
| 163 | ${filterQuery} |
no test coverage detected