This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and document custom example (close #3)
- Loading branch information
Showing
8 changed files
with
332 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
web/bigquery/definitions/custom/02_custom_example/01_link_clicks.sqlx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
Copyright 2021 Snowplow Analytics Ltd. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
config { | ||
type: "operations", | ||
name: "01_link_clicks", | ||
disabled: sp.model_disabled, | ||
dependencies: [ | ||
"07_commit_page_views" | ||
], | ||
hermetic: true, | ||
hasOutput: false, | ||
tags: ["sp_web", "sp_custom"] | ||
} | ||
|
||
CREATE OR REPLACE TABLE `${sp.scratch_schema}.link_clicks${sp.entropy}` AS( | ||
WITH click_count AS( | ||
SELECT | ||
page_view_id, | ||
COUNT(DISTINCT event_id) AS link_clicks, | ||
|
||
FROM | ||
`${sp.scratch_schema}.events_staged${sp.entropy}` | ||
WHERE | ||
event_name = 'link_click' | ||
GROUP BY 1 | ||
|
||
), first_and_last AS( | ||
SELECT | ||
page_view_id, | ||
|
||
FIRST_VALUE(unstruct_event_com_snowplowanalytics_snowplow_link_click_1_0_1.target_url) OVER(PARTITION BY page_view_id | ||
ORDER BY derived_tstamp desc | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS first_link_clicked, | ||
|
||
LAST_VALUE(unstruct_event_com_snowplowanalytics_snowplow_link_click_1_0_1.target_url) OVER(PARTITION BY page_view_id | ||
ORDER BY derived_tstamp desc | ||
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_link_clicked | ||
|
||
FROM `${sp.scratch_schema}.events_staged${sp.entropy}` | ||
WHERE event_name = 'page_view' | ||
) | ||
SELECT | ||
b.page_view_id, | ||
COALESCE(a.link_clicks, 0) AS link_clicks, | ||
b.first_link_clicked, | ||
b.last_link_clicked | ||
|
||
FROM | ||
click_count a | ||
LEFT JOIN | ||
first_and_last b | ||
ON b.page_view_id = a.page_view_id | ||
); |
84 changes: 84 additions & 0 deletions
84
web/bigquery/definitions/custom/02_custom_example/02_channel_engagement.sqlx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2021 Snowplow Analytics Ltd. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
config { | ||
type: "operations", | ||
name: "02_channel_engagement", | ||
disabled: sp.model_disabled, | ||
dependencies: [ | ||
"01_link_clicks" | ||
], | ||
hermetic: true, | ||
hasOutput: false, | ||
tags: ["sp_web", "sp_custom"] | ||
} | ||
|
||
CREATE OR REPLACE TABLE `${sp.scratch_schema}.channel_engagement_staged${sp.entropy}` AS( | ||
SELECT | ||
-- get some basic info | ||
pv.page_view_id, | ||
pv.start_tstamp, | ||
pv.page_url, | ||
|
||
-- Add in our custom link click metrics | ||
lc.link_clicks, | ||
lc.first_link_clicked, | ||
lc.last_link_clicked, | ||
|
||
-- channel definition | ||
CASE | ||
WHEN pv.refr_medium = 'search' | ||
AND (REGEXP_CONTAINS(lower(pv.mkt_medium), '%(cpc|ppc|sem|paidsearch)%') | ||
OR REGEXP_CONTAINS(lower(pv.mkt_source), '%(cpc|ppc|sem|paidsearch)%')) THEN 'paidsearch' | ||
WHEN lower(pv.mkt_medium) LIKE '%paidsearch%' | ||
OR lower(pv.mkt_source) LIKE '%paidsearch%' THEN 'paidsearch' | ||
WHEN REGEXP_CONTAINS(lower(pv.mkt_source), '%(adwords|google_paid|googleads)%') | ||
OR REGEXP_CONTAINS(lower(pv.mkt_medium), '%(adwords|google_paid|googleads)%') THEN 'paidsearch' | ||
WHEN lower(pv.mkt_source) LIKE '%google%' | ||
|
||
AND lower(pv.mkt_medium) LIKE '%ads%' THEN 'paidsearch' | ||
WHEN pv.refr_urlhost in ('www.googleadservices.com','googleads.g.doubleclick.net') then 'paidsearch' | ||
|
||
WHEN REGEXP_CONTAINS(lower(pv.mkt_medium), '%(cpv|cpa|cpp|content-text|advertising|ads)%') THEN 'advertising' | ||
WHEN REGEXP_CONTAINS(lower(pv.mkt_medium), '%(display|cpm|banner)%') THEN 'display' | ||
|
||
WHEN pv.refr_medium IS NULL AND pv.page_url NOT LIKE '%utm_%' THEN 'direct' | ||
WHEN (LOWER(pv.refr_medium) = 'search' AND pv.mkt_medium IS NULL) | ||
OR (LOWER(pv.refr_medium) = 'search' AND LOWER(pv.mkt_medium) = 'organic') THEN 'organicsearch' | ||
WHEN pv.refr_medium = 'social' | ||
OR REGEXP_CONTAINS(LOWER(pv.mkt_source),'^((.*(facebook|linkedin|instagram|insta|slideshare|social|tweet|twitter|youtube|lnkd|pinterest|googleplus|instagram|plus.google.com|quora|reddit|t.co|twitch|viadeo|xing|youtube).*)|(yt|fb|li))$') | ||
OR REGEXP_CONTAINS(LOWER(pv.mkt_medium),'^(.*)(social|facebook|linkedin|twitter|instagram|tweet)(.*)$') THEN 'social' | ||
WHEN pv.refr_medium = 'email' | ||
OR lower(pv.mkt_medium) LIKE '_mail' THEN 'email' | ||
WHEN lower(pv.mkt_medium) LIKE 'affiliate' THEN 'affiliate' | ||
WHEN pv.refr_medium = 'unknown' or lower(pv.mkt_medium) LIKE 'referral' OR lower(pv.mkt_medium) LIKE 'referal' THEN 'referral' | ||
WHEN pv.refr_medium = 'internal' then 'internal' | ||
ELSE 'others' | ||
END AS channel, | ||
|
||
-- some metrics to measure engagement | ||
CASE | ||
WHEN pv.engaged_time_in_s = 0 THEN TRUE | ||
ELSE FALSE | ||
END AS bounced_page_view, | ||
|
||
(pv.vertical_percentage_scrolled / 100) * 0.3 + (pv.engaged_time_in_s / 600) * 0.7 AS engagement_score | ||
|
||
FROM | ||
-- Query from staged table to get most recent according to incremental logic | ||
`${sp.scratch_schema}.page_views_staged${sp.entropy}` pv | ||
LEFT JOIN | ||
-- Join on the joinkey | ||
`${sp.scratch_schema}.link_clicks${sp.entropy}` lc | ||
ON pv.page_view_id = lc.page_view_id | ||
); |
35 changes: 35 additions & 0 deletions
35
web/bigquery/definitions/custom/02_custom_example/03_commit_custom.sqlx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
Copyright 2021 Snowplow Analytics Ltd. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
config { | ||
type: "operations", | ||
name: "03_commit_custom", | ||
disabled: sp.model_disabled, | ||
dependencies: [ | ||
"02_channel_engagement" | ||
], | ||
hermetic: true, | ||
hasOutput: false, | ||
tags: ["sp_web", "sp_custom"] | ||
} | ||
|
||
-- Commit table procedure handles committing to prod, including table creation, and creation of new columns if 'automigrate' is set to TRUE | ||
|
||
CALL `${sp.output_schema}.commit_table` ( | ||
'${sp.scratch_schema}', -- sourceDataset | ||
'channel_engagement_staged${sp.entropy}', -- sourceTable | ||
'${sp.output_schema}', -- targetDataset | ||
'channel_engagement${sp.entropy}', -- targetTable | ||
'page_view_id', -- joinKey | ||
'start_tstamp', -- partitionKey | ||
TRUE); -- automigrate |
29 changes: 29 additions & 0 deletions
29
web/bigquery/definitions/custom/02_custom_example/99_custom_cleanup.sqlx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2021 Snowplow Analytics Ltd. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
config { | ||
type: "operations", | ||
name: "99_custom_cleanup", | ||
disabled: sp.model_disabled && sp.destroy_disabled, | ||
dependencies: [ | ||
"03_commit_custom" | ||
], | ||
hermetic: true, | ||
hasOutput: false, | ||
tags: ["sp_web", "sp_custom"] | ||
} | ||
|
||
-- We can run this cleanup straight after the commit step, as long as no subsequent logic depends on it. | ||
|
||
DROP TABLE IF EXISTS `${sp.scratch_schema}.link_clicks${sp.entropy}`; | ||
DROP TABLE IF EXISTS `${sp.scratch_schema}.channel_engagement_staged${sp.entropy}`; |
26 changes: 26 additions & 0 deletions
26
web/bigquery/definitions/custom/02_custom_example/XX_destroy_custom.sqlx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
Copyright 2021 Snowplow Analytics Ltd. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
config { | ||
type: "operations", | ||
name: "XX_destroy_custom", | ||
disabled: sp.destroy_disabled, | ||
dependencies: [ | ||
"99_custom_cleanup" | ||
], | ||
hermetic: true, | ||
hasOutput: false, | ||
tags: ["sp_custom"] | ||
} | ||
|
||
DROP TABLE IF EXISTS `${sp.output_schema}.channel_engagement${sp.entropy}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters