├── .github
├── CODEOWNERS
└── workflows
│ └── ci-check.yml
├── .gitignore
├── LICENSE
├── config_templates
├── attribution_rules.csv
├── attribution_windows.csv
├── conversion_rules.csv
├── conversion_shares.csv
└── touch_rules.csv
├── dbt_project.yml
├── docs
└── configuration.md
├── integration_tests
├── .sqlfluff
├── .sqlfluffignore
├── Makefile
├── data
│ ├── .gitkeep
│ ├── attribution_rules.csv
│ ├── attribution_windows.csv
│ ├── conversion_rules.csv
│ ├── conversion_shares.csv
│ ├── dummy_data
│ │ ├── conversion_events.csv
│ │ └── touch_events.csv
│ └── touch_rules.csv
├── dbt_project.yml
├── macros
│ └── generate_schema_name.sql
├── models
│ ├── stg_conversion_events.sql
│ └── stg_touch_events.sql
├── package-lock.yml
├── packages.yml
├── poetry.lock
├── profiles.yml
├── pyproject.toml
└── tests
│ ├── .gitkeep
│ └── attributed_conversions
│ ├── test_conversion_shares.sql
│ ├── test_scenario_1_first_touch.sql
│ └── test_scenario_2_first_touch_7_day.sql
├── macros
├── .gitkeep
├── current_utc_time.sql
├── generate_surrogate_key.sql
├── generate_uuid.sql
├── get_warehouse.sql
├── test_greater_than_zero.sql
└── test_in_past.sql
├── models
├── schema.yml
├── tasman_mta__attributed_conversions.sql
├── tasman_mta__attributed_touches.sql
├── tasman_mta__filtered_conversion_events.sql
├── tasman_mta__filtered_touch_events.sql
└── tasman_mta__performance_history.sql
└── readme.md
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | # This CODEOWNERS file defines ownership of specific folders within this repo
2 | # Each line is a file pattern followed by one or more owners.
3 |
4 | # These owners will be the default owners for everything in
5 | # the repo. Unless a later match takes precedence,
6 | # these teams/reviewers will be requested for
7 | # review when someone opens a pull request.
8 | * @TasmanAnalytics/analytics-engineering
9 |
10 |
11 | # All files in the below directory and any of its
12 | # subdirectories will be owned by the specified team
13 |
--------------------------------------------------------------------------------
/.github/workflows/ci-check.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on:
4 | pull_request:
5 |
6 | # Automatically cancel any previous runs of this workflow
7 | concurrency:
8 | group: ${{ github.workflow }}-${{ github.ref }}
9 | cancel-in-progress: true
10 |
11 | jobs:
12 | ci-check-snowflake:
13 | runs-on: ubuntu-latest
14 | defaults:
15 | run:
16 | working-directory: ./integration_tests
17 | env:
18 | SNOWFLAKE_ACCOUNT: ${{ secrets.SNOWFLAKE_ACCOUNT }}
19 | SNOWFLAKE_USER_CI: ${{ secrets.SNOWFLAKE_CI_USER }}
20 | SNOWFLAKE_PASSWORD_CI: ${{ secrets.SNOWFLAKE_CI_USER_PASSWORD }}
21 | SNOWFLAKE_DATABASE_CI: DBT_PACKAGE_CI
22 | SNOWFLAKE_ROLE_CI: PACKAGE_CI_ROLE
23 | SNOWFLAKE_WAREHOUSE_CI: PACKAGE_CI_WH
24 | SNOWFLAKE_SCHEMA_CI: DBT_CI_${{github.event.number}}_${{ github.actor }} # Creates a unique schema for each PR
25 | steps:
26 | - name: Checkout branch
27 | id: checkout-branch
28 | uses: actions/checkout@v3
29 |
30 | - name: Install Poetry
31 | id: install-poetry
32 | run: |
33 | pipx install poetry
34 |
35 | - name: setup-python
36 | id: setup-python
37 | uses: actions/setup-python@v4
38 | with:
39 | python-version: "3.10"
40 | cache: 'poetry' # Auto cache based on poetry.lock
41 |
42 | - name: Install python deps
43 | id: install-python-deps
44 | run: |
45 | poetry install
46 |
47 | - name: Install dbt deps
48 | id: install-dbt-deps
49 | run: |
50 | poetry run dbt deps
51 |
52 | - name: Check dbt compiles and CI Profiles work
53 | id: check-dbt-ci-profiles-work
54 | run: |
55 | poetry run dbt debug --target snowflake-ci
56 |
57 | - name: dbt build on Snowflake
58 | id: dbt-build-snowflake
59 | run: |
60 | poetry run dbt build --full-refresh --target snowflake-ci
61 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by https://www.toptal.com/developers/gitignore/api/python,macos,jupyternotebooks
2 | # Edit at https://www.toptal.com/developers/gitignore?templates=python,macos,jupyternotebooks
3 |
4 | ### JupyterNotebooks ###
5 | # gitignore template for Jupyter Notebooks
6 | # website: http://jupyter.org/
7 |
8 | .ipynb_checkpoints
9 | */.ipynb_checkpoints/*
10 |
11 | # IPython
12 | profile_default/
13 | ipython_config.py
14 |
15 | # Remove previous ipynb_checkpoints
16 | # git rm -r .ipynb_checkpoints/
17 |
18 | ### macOS ###
19 | # General
20 | .DS_Store
21 | .AppleDouble
22 | .LSOverride
23 | .idea/
24 | .vscode/
25 |
26 | # Icon must end with two \r
27 | Icon
28 |
29 |
30 | # Thumbnails
31 | ._*
32 |
33 | # Files that might appear in the root of a volume
34 | .DocumentRevisions-V100
35 | .fseventsd
36 | .Spotlight-V100
37 | .TemporaryItems
38 | .Trashes
39 | .VolumeIcon.icns
40 | .com.apple.timemachine.donotpresent
41 |
42 | # Directories potentially created on remote AFP share
43 | .AppleDB
44 | .AppleDesktop
45 | Network Trash Folder
46 | Temporary Items
47 | .apdisk
48 |
49 | ### macOS Patch ###
50 | # iCloud generated files
51 | *.icloud
52 |
53 | ### Python ###
54 | # Byte-compiled / optimized / DLL files
55 | __pycache__/
56 | *.py[cod]
57 | *$py.class
58 |
59 | # C extensions
60 | *.so
61 |
62 | # Distribution / packaging
63 | .Python
64 | build/
65 | develop-eggs/
66 | dist/
67 | downloads/
68 | eggs/
69 | .eggs/
70 | lib/
71 | lib64/
72 | parts/
73 | sdist/
74 | var/
75 | wheels/
76 | share/python-wheels/
77 | *.egg-info/
78 | .installed.cfg
79 | *.egg
80 | MANIFEST
81 |
82 | # PyInstaller
83 | # Usually these files are written by a python script from a template
84 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
85 | *.manifest
86 | *.spec
87 |
88 | # Installer logs
89 | pip-log.txt
90 | pip-delete-this-directory.txt
91 |
92 | # Unit test / coverage reports
93 | htmlcov/
94 | .tox/
95 | .nox/
96 | .coverage
97 | .coverage.*
98 | .cache
99 | nosetests.xml
100 | coverage.xml
101 | *.cover
102 | *.py,cover
103 | .hypothesis/
104 | .pytest_cache/
105 | cover/
106 |
107 | # Translations
108 | *.mo
109 | *.pot
110 |
111 | # Django stuff:
112 | *.log
113 | local_settings.py
114 | db.sqlite3
115 | db.sqlite3-journal
116 |
117 | # Flask stuff:
118 | instance/
119 | .webassets-cache
120 |
121 | # Scrapy stuff:
122 | .scrapy
123 |
124 | # Sphinx documentation
125 | docs/_build/
126 |
127 | # PyBuilder
128 | .pybuilder/
129 | target/
130 | edr_target/
131 | # Jupyter Notebook
132 |
133 | # IPython
134 |
135 | # pyenv
136 | # For a library or package, you might want to ignore these files since the code is
137 | # intended to run in multiple environments; otherwise, check them in:
138 | # .python-version
139 |
140 | # pipenv
141 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
142 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
143 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
144 | # install all needed dependencies.
145 | #Pipfile.lock
146 |
147 | # poetry
148 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
149 | # This is especially recommended for binary packages to ensure reproducibility, and is more
150 | # commonly ignored for libraries.
151 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
152 | #poetry.lock
153 |
154 | # pdm
155 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
156 | #pdm.lock
157 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
158 | # in version control.
159 | # https://pdm.fming.dev/#use-with-ide
160 | .pdm.toml
161 |
162 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
163 | __pypackages__/
164 |
165 | # Celery stuff
166 | celerybeat-schedule
167 | celerybeat.pid
168 |
169 | # SageMath parsed files
170 | *.sage.py
171 |
172 | # Environments
173 | .env
174 | .venv
175 | env/
176 | venv/
177 | ENV/
178 | env.bak/
179 | venv.bak/
180 |
181 | # Spyder project settings
182 | .spyderproject
183 | .spyproject
184 |
185 | # Rope project settings
186 | .ropeproject
187 |
188 | # mkdocs documentation
189 | /site
190 |
191 | # mypy
192 | .mypy_cache/
193 | .dmypy.json
194 | dmypy.json
195 |
196 | # Pyre type checker
197 | .pyre/
198 |
199 | # pytype static type analyzer
200 | .pytype/
201 |
202 | # Cython debug symbols
203 | cython_debug/
204 |
205 | # PyCharm
206 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
207 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
208 | # and can be added to the global gitignore or merged into this file. For a more nuclear
209 | # option (not recommended) you can uncomment the following to ignore the entire idea folder.
210 | .idea/
211 |
212 | ### Python Patch ###
213 | # Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
214 | # poetry.toml
215 |
216 | # ruff
217 | .ruff_cache/
218 |
219 | # LSP config files
220 | pyrightconfig.json
221 |
222 | # End of https://www.toptal.com/developers/gitignore/api/python,macos,jupyternotebooks
223 |
224 | # dbt
225 | target/
226 | dbt_packages/
227 | logs/
228 | .user.yml
229 |
230 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
--------------------------------------------------------------------------------
/config_templates/attribution_rules.csv:
--------------------------------------------------------------------------------
1 | model_id,spec,rule,part,attribute,relation,value
2 |
--------------------------------------------------------------------------------
/config_templates/attribution_windows.csv:
--------------------------------------------------------------------------------
1 | model_id,att_window,time_seconds
2 |
--------------------------------------------------------------------------------
/config_templates/conversion_rules.csv:
--------------------------------------------------------------------------------
1 | model_id,conversion_category,rule,part,attribute,type,relation,value
2 |
--------------------------------------------------------------------------------
/config_templates/conversion_shares.csv:
--------------------------------------------------------------------------------
1 | model_id,spec,share
2 |
--------------------------------------------------------------------------------
/config_templates/touch_rules.csv:
--------------------------------------------------------------------------------
1 | model_id,touch_category,rule,part,attribute,type,relation,value
2 |
--------------------------------------------------------------------------------
/dbt_project.yml:
--------------------------------------------------------------------------------
1 | name: 'tasman_dbt_mta'
2 | version: '1.0.2'
3 |
4 | require-dbt-version: [">=1.3.0", "<2.0.0"]
5 |
6 | config-version: 2
7 |
8 | target-path: "target"
9 | clean-targets: ["target", "dbt_modules", "dbt_packages"]
10 | macro-paths: ["macros"]
11 | log-path: "logs"
12 |
13 | models:
14 | tasman_dbt_mta:
15 | +schema: tasman_mta
--------------------------------------------------------------------------------
/docs/configuration.md:
--------------------------------------------------------------------------------
1 | - [Configuring the Engine](#configuring-the-engine)
2 | - [Configuring the Models](#configuring-the-models)
3 | - [Configuration Templates](#configuration-templates)
4 | - [Touch and Conversion Rules](#touch-and-conversion-rules)
5 | - [Touch Rules Example](#touch-rules-example)
6 | - [Conversion Rules Example](#conversion-rules-example)
7 | - [Attribution Rules](#attribution-rules)
8 | - [Attribution Rules Example](#attribution-rules-example)
9 | - [Conversion Shares](#conversion-shares)
10 | - [Conversion Share Example](#conversion-share-example)
11 | - [Attribution Windows](#attribution-windows)
12 | - [Attribution Window Example](#attribution-window-example)
13 | - [Touches vs Sessions](#touches-vs-sessions)
14 |
15 |
16 | # Configuring the Engine
17 |
18 | The engine can be connected to your existing touch and conversion data sources using variables within the main project `dbt_project.yml` file
19 |
20 | ```
21 | vars:
22 | tasman_dbt_mta:
23 | incremental: ""
24 | touches_model: "{{ ref() }}"
25 | touches_event_id_field: ""
26 | touches_timestamp_field: ""
27 | touches_user_id_field: ""
28 | conversions_model: "{{ ref()}}"
29 | conversions_event_id_field: ""
30 | conversions_timestamp_field: ""
31 | conversions_user_id_field: ""
32 | conversion_rules: "{{ ref() }}"
33 | touch_rules: "{{ ref() }}"
34 | attribution_rules: "{{ ref() }}"
35 | conversion_shares: "{{ ref() }}"
36 | attribution_windows: "{{ ref() }}"
37 | snowflake_prod_warehouse: ""
38 | snowflake_dev_warehouse: ""
39 | ```
40 |
41 | - **`incremental`:** "true" or "false" depending on whether the model should run using incremental models or not
42 | - **`touches_model`:** Reference to the model containing touch data points. This can be touches or sessions - [read more here](#touches-vs-sessions).
43 | - **`touches_timestamp_field`:** Field within the `touches_model` that contains timestamps for each touch point.
44 | - Touches must occur in the past, and there are column tests throughout the package to validate this.
45 | - **`touches_event_id_field`:** Field within the `touches_model` that contains a unique indentifier for each touch point
46 | - **`touches_user_id_field`:** Field within the `touches_model` that contains the user identifier
47 | - **`conversions_model`:** Reference to the model containing conversion data points
48 | - **`conversions_timestamp_field`:** Field within the `conversions_model` that contains timestamps for each conversion.
49 | - Conversions must occur in the past, and there are column tests throughout the package to validate this.
50 | - **`conversions_event_id_field`:** Field within the `conversions_model` that contains a unique indentifier for each conversion
51 | - **`conversions_user_id_field`:** Field within the `conversions_model` that contains the user identifier
52 | - **`conversion_rules`:** A seed file containing rules that can be used to filter specific conversions for each attribution model
53 | - **`touch_rules`:** A seed file containing rules that can be used to filter specific touches for each attribution model
54 | - **`attribution_rules`:** A seed file containing rules that are used to determine how touches are attributed to conversions (specs) for each attribution model
55 | - **`conversion_shares`:** A seed file that maps to each attribution spec to determine the credit awarded to touches meeting each rule for each attribution model
56 | - **`attribution_windows`:** A seed file that determines the maximum time between a touch and its conversion for each attribution model
57 | - **`snowflake_prod_warehouse`:** **(Snowflake connections only)** This is the snowflake warehouse that should be used for when the target = 'prod'. An empty string will use the profile default warehouse.
58 | - **`snowflake_dev_warehouse`:** **(Snowflake connections only)** This is the snowflake warehouse that should be used for when the target = 'dev'. An empty string will use the profile default warehouse.
59 |
60 |
61 | # Configuring the Models
62 |
63 | Consistent across all files is the `model_id` field, which described which attribution model the configuration relates to. This is a string field, and will appear alongside the attributed conversions in the output tables, and therefore, it is good to give each model a useful or relevant name that ensures uniqueness. For a last touch model, with a 30 day attribution window on a payment conversion, this might be `last_touch_30_days_payment`
64 |
65 | ## Configuration Templates
66 | Templated seed files (csvs) containing the required schema are included in the [`config_templates`](../config_templates/) folder. These must be copied to the appropriate data or seeds folder within the top-level dbt project.
67 |
68 | ## Touch and Conversion Rules
69 |
70 | These files contains rules that are used to filter touches and conversions for specific attribution models.
71 | > N.B. There needs to be at least 1 rule per model for that model to receive any touches or conversions (otherwise they are all filtered out).
72 |
73 | ### Touch Rules Example
74 | > N.B. line spaces are for readability - they should not be included in the actual seed file
75 |
76 | ```
77 | model_id,touch_category,rule,part,attribute,type,relation,value
78 |
79 | first_touch_lead_7_days,all_channels,1,1,touch_channel,string,<>,''
80 |
81 | last_touch_purchase_30_days,all_channels,1,1,touch_channel,string,<>,''
82 |
83 | u_shaped_purchase_all_time,all_channels,1,1,touch_channel,string,<>,''
84 |
85 | w_shaped_30_days,all_channels,1,1,touch_channel,string,<>,''
86 | ```
87 |
88 | ### Conversion Rules Example
89 | > N.B. line spaces are for readability - they should not be included in the actual seed file
90 | ```
91 | model_id,conversion_category,rule,part,attribute,type,relation,value
92 |
93 | first_touch_lead_7_days,purchase,1,1,conversion_type,string,=,purchase
94 |
95 | last_touch_purchase_30_days,purchase,1,1,conversion_type,string,=,lead
96 |
97 | u_shaped_purchase_all_time,purchase,1,1,conversion_type,string,=,purchase
98 |
99 | w_shaped_30_days,lead,1,1,conversion_type,string,=,lead
100 | w_shaped_30_days,purchase,1,1,conversion_type,string,=,purchase
101 | ```
102 |
103 | **Schema:**
104 | - **`model_id`:** The identifier for the attribution model that the rule corresponds to.
105 | - **`touch_category` / `conversion_category`:** A text field that can be used to describe the category of touches or conversions for the model. This provides a mechanism to add additional attribution specific categorisations to the touches and conversions.
106 | - **`rule`:** A 1-indexed integer defining the rule number for that touch category. Each rule is evaluated with OR logic, so if a category has 2 rules, the logic is rule 1 OR rule 2 has to be met for the touch to be assigned that category.
107 | - **`part`:** A 1-indexed integer defining parts of a rule. Each rule part is considered with AND logic, so if a rule has 2 parts, the logic is part 1 AND part 2 has to be met for the touch to be evaluated **true** against that rule.
108 | - **`attribute`:** The field within the `touches_model` or `conversions_model` that is being evaluated for the rule part. If the attribute doesn't match any fields in the model then no rows will be matched.
109 | - **`type`:** The data type of the attribute field within the `touches_model` or `conversions_model`. This is important to enable correct casting of the value evaluated against the attribute.
110 | - **`relation`:** The SQL boolean logic operator used to evalute the attribute and value.
111 | - **`value`:** The value evaluted for the rule part. Empty strings can be a value but required empty quotes as in the example above.
112 |
113 | ## Attribution Rules
114 |
115 | The attribution rules seed defines how touches are attributed to conversions for each attribution model. Each set of rules is grouped into a **spec**, and each spec can be assigned a different conversion share value in the conversion shares seed.
116 |
117 | ### Attribution Rules Example
118 | > N.B. line spaces are for readability - they should not be included in the actual seed file
119 |
120 | ```
121 | model_id,spec,rule,part,attribute,relation,value
122 |
123 | first_touch_lead_7_days,1,1,1,convert_seq_up,=,1
124 |
125 | last_touch_purchase_30_days,1,1,1,convert_seq_down,=,1
126 |
127 | u_shaped_purchase_all_time,1,1,1,convert_seq_up,=,1
128 | u_shaped_purchase_all_time,2,1,1,convert_seq_down,=,1
129 | u_shaped_purchase_all_time,3,1,1,convert_seq_up,>,1
130 | u_shaped_purchase_all_time,3,1,2,convert_seq_down,<,1
131 |
132 | w_shaped_30_days,1,1,1,convert_seq_up,=,1
133 | w_shaped_30_days,1,1,2,conversion_category,=,lead
134 | w_shaped_30_days,2,1,1,convert_seq_down,=,1
135 | w_shaped_30_days,2,1,2,conversion_category,=,lead
136 | w_shaped_30_days,3,1,1,convert_seq_down,=,1
137 | w_shaped_30_days,3,1,2,conversion_category,=,purchase
138 | w_shaped_30_days,4,1,1,convert_seq_up,>,1
139 | w_shaped_30_days,4,1,2,convert_seq_down,>,1
140 | w_shaped_30_days,4,1,3,conversion_category,=,lead
141 | w_shaped_30_days,4,2,1,convert_seq_down,>,1
142 | w_shaped_30_days,4,2,2,conversion_category,=,purchase
143 |
144 | ```
145 | **Schema:**
146 | - **`model_id`:** The identifier for the attribution model that the rule corresponds to.
147 | - **`spec`:** Short for specification, each spec defines the rule set of a particular attribution model, and can be assigned a conversion share value. In the example above, it can be seen that 'single touch' models such as first touch and last touch only have 1 spec, whereas more complex multi-touch or multi-conversion models will have more than one spec.
148 | > N.B. where a spec matches than one touch, the conversion share is split equally between the touches.
149 |
150 | - **`rule`:** A 1-indexed integer defining the rule number for that spec. Each rule is evaluated with OR logic, so if a category has 2 rules, the logic is rule 1 OR rule 2 has to be met for the touch to be assigned that category.
151 | - **`part`:** A 1-indexed integer defining parts of a rule. Each rule part is considered with AND logic, so if a rule has 2 parts, the logic is part 1 AND part 2 has to be met for the touch to be evaluated **true** against that rule.
152 | - **`attribute`**: The derived property that is being evaluated for the rule part. If the attribute doesn't match any fields in the model then logically is will always output **false**. Properties available are:
153 | - `touch_category`: The category of the touch as per the touch rules
154 | - `conversion_category`: The category of the conversion as per the conversion rules
155 | - `convert_touch_count`: The total number of attributed touches.
156 | - `convert_seq_up`: The consecutive touch number based on the timestamp ascending.
157 | - `convert_seq_down`: The consecutive touch number based on the timestamp descending.
158 | - `interval_pre`: Time in seconds between the touch and the touch preceding.
159 | - `interval_post`: Time in seconds between the touch and the touch following.
160 | - `interval_convert`: Time in seconds between the touch and the attributed conversion.
161 |
162 | >The 'convert_seq' properties are used when the attribution rules are positional - such as first touch, last touch, u-shaped, w-shaped models.
163 | >The 'interval' properties are used when the attribution rules are time-based - such as a decay model.
164 |
165 | - **`relation`:** The SQL boolean logic operator used to evalute the attribute and value.
166 | - **`value`:** The value evaluted for the rule part.
167 |
168 | ## Conversion Shares
169 |
170 | The conversion shares seed is used to map attribution rules specs to decimal percentage conversion credits that are applied to matching touches.
171 |
172 | ### Conversion Share Example
173 | > N.B. line spaces are for readability - they should not be included in the actual seed file
174 | ```
175 | model_id,spec,share
176 |
177 | first_touch_lead_7_days,1,1
178 |
179 | last_touch_purchase_30_days,1,1
180 |
181 | u_shaped_purchase_all_time,1,0.4
182 | u_shaped_purchase_all_time,2,0.4
183 | u_shaped_purchase_all_time,3,0.2
184 |
185 | w_shaped_30_days,1,0.3
186 | w_shaped_30_days,2,0.3
187 | w_shaped_30_days,3,0.3
188 | w_shaped_30_days,4,0.1
189 | ```
190 | **Schema:**
191 | - **`model_id`:** The identifier for the attribution model that the rule corresponds to.
192 | - **`spec`:** The spec within the attribution rules seed that the share is to be applied to.
193 | - **`share`:** The decimal percentage share that is granted to touches matching that spec. This share is split equally between all matching touches.
194 |
195 | > Example U-Shaped model
196 | > 6 touches happen before conversion, the shares are split as follows:
197 | > - Spec 1: First touch (Touch 1) = 40% share
198 | > - Spec 2: Last Touch (Touch 6) = a 40% share
199 | > - Spec 3: All other touches (Touches 2,3,4,5) split a 20% share = 5% each
200 |
201 | > Example W-Shaped model
202 | > 3 touches happen before lead conversion, 4 touches happen inbetween lead conversion and purchase conversion. The shares are split as follows:
203 | > - Spec 1: First touch (Touch 1) = 30% share
204 | > - Spec 2: Last touch before lead (Touch 3) = 30%
205 | > - Spec 3: Last touch before purchase (Touch 7) = 30%
206 | > - Spec 4: All other touches (Touches 2,4,5,6) split a 10% share = 2.5% each
207 |
208 | ## Attribution Windows
209 |
210 | The attribution window seed is used to define the maximum time between a touch and conversion for each attribution model.
211 |
212 | ### Attribution Window Example
213 | > N.B. line spaces are for readability - they should not be included in the actual seed file
214 | ```
215 | model_id,att_window,time_seconds
216 |
217 | first_touch_lead_7_days,7 day,604800
218 |
219 | last_touch_purchase_30_days,30 day,2629746
220 |
221 | u_shaped_purchase_all_time,all time,0
222 |
223 | w_shaped_30_days,30 day,2629746
224 | ```
225 | **Schema:**
226 | - **`model_id`:** The identifier for the attribution model that the rule corresponds to.
227 | - **`att_window`:** A string field used to describe the attribution window as plain text. This is passed as metadata in the output table as additional context.
228 | - **`time_seconds`:** The maximum time in seconds allowed between a touch and conversion.
229 |
230 |
231 | # Touches vs Sessions
232 | The term used throughout this package to describe the actions taken by a given user is a touch. However, many organisations prefer to think about attribution from a session perspective. The engine supports both and isn't opionated in its approach.
233 |
234 | If working with individual touches, its important to filter out touches that come from an internal referrer - particularly when using a last-touch model - otherwise the last touch will almost always be an internal touch and provide little insight.
235 |
236 | If working with sessions, its important that sessionisation is completed upstream of the engine, and that the model contains 1 row per session.
--------------------------------------------------------------------------------
/integration_tests/.sqlfluff:
--------------------------------------------------------------------------------
1 | [sqlfluff]
2 | dialect = snowflake
3 | templater = dbt
4 | exclude_rules = structure.column_order, convention.not_equal, references.keywords, aliasing.unused
5 | max_line_length = 500
6 |
7 | [sqlfluff:templater:dbt]
8 | project_dir = ./
9 | profile = tasman_integration_tests
10 |
11 | [sqlfluff:templater:jinja]
12 | apply_dbt_builtins = True
13 |
14 | [sqlfluff:indentation]
15 | indented_on_contents = False
16 | template_blocks_indent = False
17 | tab_space_size = 4
18 | indent_unit = space
19 |
20 | [sqlfluff:rules:capitalisation.keywords]
21 | # Keywords
22 | capitalisation_policy = lower
23 |
24 | [sqlfluff:rules:capitalisation.identifiers]
25 | # Unquoted identifiers
26 | extended_capitalisation_policy = lower
27 |
28 | [sqlfluff:rules:layout.long_lines]
29 | # Line length
30 | ignore_comment_lines = True
31 | ignore_comment_clauses = True
32 |
33 | [sqlfluff:rules:capitalisation.functions]
34 | # Function names
35 | extended_capitalisation_policy = lower
36 |
37 | [sqlfluff:rules:capitalisation.literals]
38 | # Null & Boolean Literals
39 | capitalisation_policy = lower
40 |
41 | [sqlfluff:rules:ambiguous.column_references]
42 | # GROUP BY/ORDER BY column references
43 | group_by_and_order_by_style = explicit
44 |
45 | [sqlfluff:rules:references.special_chars]
46 | # Special characters in identifiers
47 | unquoted_identifiers_policy = all
48 | quoted_identifiers_policy = none
49 | allow_space_in_identifier = False
50 |
51 | [sqlfluff:rules:capitalisation.types]
52 | # Data Types
53 | extended_capitalisation_policy = lower
54 |
55 | [sqlfluff:rules:convention.casting_style]
56 | # SQL type casting
57 | preferred_type_casting_style = shorthand
--------------------------------------------------------------------------------
/integration_tests/.sqlfluffignore:
--------------------------------------------------------------------------------
1 | target/
2 | dbt_packages/
3 | macros/
4 | .venv/
--------------------------------------------------------------------------------
/integration_tests/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: dbt docs lint
2 | .DEFAULT_GOAL := help
3 |
4 | # Makes all arguments after the `lint` command do-nothing targets
5 | ifeq (lint,$(firstword $(MAKECMDGOALS)))
6 | RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
7 | $(eval $(RUN_ARGS):;@:)
8 | endif
9 |
10 | # Initialisation recipes
11 | poetry: ## Install poetry
12 | @if ! command -v poetry; then\
13 | curl -sSL https://install.python-poetry.org | python3 -;\
14 | fi
15 | poetry install --directory ../
16 |
17 | # dbt Development recipes
18 | dbt: poetry ## Start a dbt shell
19 | export DBT_PROFILES_DIR=~/.dbt/ && export SHELL=/bin/zsh && poetry shell
20 |
21 | docs: poetry ## Compile the dbt project & start dbt docs
22 | poetry run dbt docs generate --profiles-dir ~/.dbt/
23 | poetry run dbt docs serve
24 |
25 | lint: poetry ## SQLFluff lint the dbt project (run `make lint ` to lint specific paths)
26 | poetry run sqlfluff lint --config ../.sqlfluff $(RUN_ARGS)
27 |
28 |
29 | lint-fix: poetry ## SQLFluff lint the dbt project (run `make lint ` to lint specific paths)
30 | poetry run sqlfluff fix --config ../.sqlfluff $(RUN_ARGS)
31 |
32 | clean: ## Uninstall the dbt virtual environment
33 | @echo Uninstalling the Poetry virtual environment.
34 | poetry env remove python || rm -rf ../.venv
35 |
36 | help: ## Show targets and comments (must have ##)
37 | @fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
38 |
--------------------------------------------------------------------------------
/integration_tests/data/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TasmanAnalytics/tasman-dbt-mta/78f49cd8c4fccdf696506aec49af99b71f232bd4/integration_tests/data/.gitkeep
--------------------------------------------------------------------------------
/integration_tests/data/attribution_rules.csv:
--------------------------------------------------------------------------------
1 | model_id,spec,rule,part,attribute,relation,value
2 | first_touch,1,1,1,convert_seq_up,=,1
3 | first_touch_7_day,1,1,1,convert_seq_up,=,1
4 | first_touch_30_day,1,1,1,convert_seq_up,=,1
5 | w_shaped_30_days,1,1,1,convert_seq_up,=,1
6 | w_shaped_30_days,1,1,2,conversion_category,=,lead
7 | w_shaped_30_days,2,1,1,convert_seq_down,=,1
8 | w_shaped_30_days,2,1,2,conversion_category,=,lead
9 | w_shaped_30_days,3,1,1,convert_seq_down,=,1
10 | w_shaped_30_days,3,1,2,conversion_category,=,purchase
11 | w_shaped_30_days,4,1,1,convert_seq_up,>,1
12 | w_shaped_30_days,4,1,2,convert_seq_down,>,1
13 | w_shaped_30_days,4,1,3,conversion_category,=,lead
14 | w_shaped_30_days,4,2,1,convert_seq_down,>,1
15 | w_shaped_30_days,4,2,2,conversion_category,=,purchase
--------------------------------------------------------------------------------
/integration_tests/data/attribution_windows.csv:
--------------------------------------------------------------------------------
1 | model_id,att_window,time_seconds
2 | first_touch,all_time,0
3 | first_touch_7_day,7 day,604800
4 | first_touch_30_day,30 day,2629746
5 | w_shaped_30_days,30 day,2629746
--------------------------------------------------------------------------------
/integration_tests/data/conversion_rules.csv:
--------------------------------------------------------------------------------
1 | model_id,conversion_category,rule,part,attribute,type,relation,value
2 | first_touch,purchase,1,1,conversion_type,string,=,purchase
3 | first_touch_7_day,purchase,1,1,conversion_type,string,=,purchase
4 | first_touch_30_day,purchase,1,1,conversion_type,string,=,purchase
5 | w_shaped_30_days,lead,1,1,conversion_type,string,=,lead
6 | w_shaped_30_days,purchase,1,1,conversion_type,string,=,purchase
--------------------------------------------------------------------------------
/integration_tests/data/conversion_shares.csv:
--------------------------------------------------------------------------------
1 | model_id,spec,share
2 | first_touch,1,1
3 | first_touch_7_day,1,1
4 | first_touch_30_day,1,1
5 | w_shaped_30_days,1,0.3
6 | w_shaped_30_days,2,0.3
7 | w_shaped_30_days,3,0.3
8 | w_shaped_30_days,4,0.1
--------------------------------------------------------------------------------
/integration_tests/data/dummy_data/conversion_events.csv:
--------------------------------------------------------------------------------
1 | event,event_timestamp,user,type
2 | 739982bb-f169-40d0-814d-ff633db562c0,2022-05-12 00:54:19,user1@tasman.ai,purchase
3 | 1e0c4442-03ef-40bd-a5b8-2cfc77457903,2022-05-21 11:31:37,user2@tasman.ai,purchase
4 | da9360ef-f52e-4572-aa98-128c17d97a41,2022-06-07 07:08:37,user3@tasman.ai,lead
5 | be62daba-e4d2-4998-a057-8a3b25f2e9e3,2022-06-17 06:48:26,user3@tasman.ai,purchase
6 | 3olgh3bs-05hf-40bd-a5b8-2cfc77457903,2024-09-12 11:31:37,user5@tasman.ai,purchase
--------------------------------------------------------------------------------
/integration_tests/data/dummy_data/touch_events.csv:
--------------------------------------------------------------------------------
1 | event,event_timestamp,user,channel
2 | 3f6931bc-785c-46fa-b868-6cac0bac549e,2022-04-01 05:44:29,user1@tasman.ai,paid-search
3 | 00df6f46-39eb-48e8-84e5-45d7abed2502,2022-04-15 12:30:23,user1@tasman.ai,direct
4 | cb3a11ab-61ae-4e09-a1c6-8a49c44aa6ae,2022-05-06 00:54:19,user1@tasman.ai,direct
5 | 97baafbf-02d7-4f11-b3a6-35a5421ad227,2022-05-11 20:30:55,user1@tasman.ai,organic-search
6 | 34696b23-4ac2-48aa-9ddc-4846209a989f,2022-05-12 00:30:10,user1@tasman.ai,paid-social
7 | 1b9241c8-7516-46d2-af13-fe0e309b997b,2022-05-19 09:15:56,user2@tasman.ai,paid-search
8 | f3457cae-84ee-484c-b4d1-cf4fd7d0b867,2022-05-20 11:30:12,user2@tasman.ai,organic-search
9 | b77dc2e5-847c-45d0-97c1-d2acd1d23593,2022-05-21 11:30:00,user2@tasman.ai,paid-search
10 | 564e4817-ac4d-486f-b2aa-e0410e585347,2021-06-07 08:08:41,user3@tasman.ai,organic-search
11 | 42dbe46e-698c-42c5-94d9-0c6a9857f56d,2022-05-08 10:10:10,user3@tasman.ai,paid-search
12 | 536a69ee-1c0d-4287-a1b8-76d4dd17ea96,2022-06-07 06:10:56,user3@tasman.ai,paid-social
13 | 536a69ee-1c0d-4287-a1b8-76d4dd17ea97,2022-06-07 06:20:56,user3@tasman.ai,organic-search
14 | 536a69ee-1c0d-4287-a1b8-76d4dd17ea98,2022-06-07 06:40:56,user3@tasman.ai,email
15 | 283d145e-4041-44b1-b239-2da1216ab6c2,2022-06-07 07:10:50,user3@tasman.ai,email
16 | e158c32b-642e-4133-84e7-2bfb6fb4ed42,2022-06-10 20:50:26,user3@tasman.ai,paid-social
17 | dff554e6-2f53-4eb1-a663-70d33658be63,2022-06-17 06:30:15,user3@tasman.ai,direct
18 | a36aa6c8-ecab-4c4b-a2be-6c615cc69c5d,2022-04-17 10:30:25,user4@tasman.ai,paid-search
19 | 50f9def3-6e57-4e28-ae58-2c887e6e9e52,2022-04-18 10:30:25,user4@tasman.ai,organic-search
20 | dedd99d9-7f58-4a92-b406-32e60c1ecbdf,2022-04-19 10:30:25,user4@tasman.ai,paid-search
21 | 2ab3jfy0-7516-46d2-af13-fe0e309b997b,2024-09-10 09:15:56,user5@tasman.ai,paid-search
22 | ef9028hg-84ee-484c-b4d1-cf4fd7d0b867,2024-09-11 11:30:12,user5@tasman.ai,organic-search
23 | 789nghfs-847c-45d0-97c1-d2acd1d23593,2024-09-11 11:30:00,user5@tasman.ai,paid-search
--------------------------------------------------------------------------------
/integration_tests/data/touch_rules.csv:
--------------------------------------------------------------------------------
1 | model_id,touch_category,rule,part,attribute,type,relation,value
2 | first_touch,all_channels,1,1,touch_channel,string,<>,''
3 | first_touch_7_day,all_channels,1,1,touch_channel,string,<>,''
4 | first_touch_30_day,all_channels,1,1,touch_channel,string,<>,''
5 | w_shaped_30_days,all_channels,1,1,touch_channel,string,<>,''
6 | first_touch_2022_paid_search,all_channels_2022_onwards,1,1,touch_channel,string,<>,'paid-search'
7 | first_touch_2022_paid_search,all_channels_2022_onwards,1,2,touch_timestamp,string,>=,'2022-01-01'
8 | first_touch_2022_paid_search,all_channels_2022_onwards,1,3,touch_timestamp,string,<=,'2022-12-31'
--------------------------------------------------------------------------------
/integration_tests/dbt_project.yml:
--------------------------------------------------------------------------------
1 |
2 | name: 'tasman_dbt_mta_integration_tests'
3 | version: '1.0.0'
4 | config-version: 2
5 |
6 | profile: 'tasman_integration_tests'
7 |
8 | model-paths: ["models"]
9 | analysis-paths: ["analyses"]
10 | test-paths: ["tests"]
11 | seed-paths: ["data"]
12 | macro-paths: ["macros"]
13 | snapshot-paths: ["snapshots"]
14 |
15 | target-path: "target"
16 | clean-targets: ["target", "dbt_packages"]
17 |
18 |
19 | models:
20 | tasman_dbt_mta_integration_tests:
21 |
22 | vars:
23 | tasman_dbt_mta:
24 | incremental: "true"
25 | touches_model: "{{ ref('stg_touch_events') }}"
26 | touches_event_id_field: "touch_event_id"
27 | touches_timestamp_field: "touch_timestamp"
28 | touches_user_id_field: "user_id"
29 | conversions_model: "{{ ref('stg_conversion_events')}}"
30 | conversions_event_id_field: "conversion_event_id"
31 | conversions_timestamp_field: "conversion_timestamp"
32 | conversions_user_id_field: "user_id"
33 | conversion_rules: "{{ ref('conversion_rules') }}"
34 | touch_rules: "{{ ref('touch_rules') }}"
35 | attribution_rules: "{{ ref('attribution_rules') }}"
36 | conversion_shares: "{{ ref('conversion_shares') }}"
37 | attribution_windows: "{{ ref('attribution_windows') }}"
38 | test_hours: "36"
39 | snowflake_prod_warehouse: ""
40 | snowflake_dev_warehouse: ""
--------------------------------------------------------------------------------
/integration_tests/macros/generate_schema_name.sql:
--------------------------------------------------------------------------------
1 | {% macro generate_schema_name(custom_schema_name, node) -%}
2 | {{ generate_schema_name_for_env(custom_schema_name, node) }}
3 | {%- endmacro %}
--------------------------------------------------------------------------------
/integration_tests/models/stg_conversion_events.sql:
--------------------------------------------------------------------------------
1 | select
2 | {{ dbt.safe_cast("event", "string") }} as conversion_event_id,
3 | event_timestamp as conversion_timestamp,
4 | {{ dbt.safe_cast("user", "string") }} as user_id,
5 | {{ dbt.safe_cast("type", "string") }} as conversion_type
6 | from
7 | {{ ref('conversion_events') }}
8 |
--------------------------------------------------------------------------------
/integration_tests/models/stg_touch_events.sql:
--------------------------------------------------------------------------------
1 | select
2 | {{ dbt.safe_cast("event", "string") }} as touch_event_id,
3 | event_timestamp as touch_timestamp,
4 | {{ dbt.safe_cast("user", "string") }} as user_id,
5 | {{ dbt.safe_cast("channel", "string") }} as touch_channel
6 | from
7 | {{ ref('touch_events') }}
8 |
--------------------------------------------------------------------------------
/integration_tests/package-lock.yml:
--------------------------------------------------------------------------------
1 | packages:
2 | - local: ../
3 | sha1_hash: de2deba3d66ce03d8c02949013650cc9b94f6030
4 |
--------------------------------------------------------------------------------
/integration_tests/packages.yml:
--------------------------------------------------------------------------------
1 | packages:
2 | - local: ../
--------------------------------------------------------------------------------
/integration_tests/profiles.yml:
--------------------------------------------------------------------------------
1 | tasman_integration_tests:
2 | outputs:
3 | snowflake-ci:
4 | account: "{{ env_var('SNOWFLAKE_ACCOUNT') }}"
5 | client_session_keep_alive: true
6 | database: "{{ env_var('SNOWFLAKE_DATABASE_CI') }}"
7 | user: "{{ env_var('SNOWFLAKE_USER_CI') }}"
8 | password: "{{ env_var('SNOWFLAKE_PASSWORD_CI') }}"
9 | role: "{{ env_var('SNOWFLAKE_ROLE_CI') }}"
10 | schema: "{{ env_var('SNOWFLAKE_SCHEMA_CI') }}"
11 | warehouse: "{{ env_var('SNOWFLAKE_WAREHOUSE_CI') }}"
12 | threads: 12
13 | type: snowflake
14 |
--------------------------------------------------------------------------------
/integration_tests/pyproject.toml:
--------------------------------------------------------------------------------
1 | [tool.poetry]
2 | name = "tasman_dbt_mta_integration_tests"
3 | version = "0.1.0"
4 | description = "Integration tests for Tasman dbt MTA Engine"
5 | authors = ["Tasman Data Engineering"]
6 | readme = "README.md"
7 |
8 | [tool.poetry.dependencies]
9 | python = "^3.10"
10 | isort = "^5.12.0"
11 | sqlparse = "^0.5.0"
12 |
13 | [tool.poetry.group.dbt.dependencies]
14 | dbt-snowflake = "~1.5.4"
15 | dbt-bigquery = "~1.5.4"
16 |
17 | [tool.poetry.group.dev.dependencies]
18 | sqlfluff = "^2.3.0"
19 | sqlfluff-templater-dbt = "^2.3.0"
20 |
21 | [tool.flake8]
22 | max-line-length = 120
23 | extend-ignore = ["E203", "W503"]
24 | extend-exclude = [".venv/"]
25 |
26 | [build-system]
27 | requires = ["poetry-core"]
28 | build-backend = "poetry.core.masonry.api"
29 |
--------------------------------------------------------------------------------
/integration_tests/tests/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TasmanAnalytics/tasman-dbt-mta/78f49cd8c4fccdf696506aec49af99b71f232bd4/integration_tests/tests/.gitkeep
--------------------------------------------------------------------------------
/integration_tests/tests/attributed_conversions/test_conversion_shares.sql:
--------------------------------------------------------------------------------
1 | select
2 | conversion_user_id,
3 | model_id,
4 | sum(conversion_share) as total_conversion_share
5 | from
6 | {{ ref('tasman_mta__attributed_conversions') }}
7 |
8 | group by
9 | conversion_user_id,
10 | model_id
11 |
12 | having
13 | total_conversion_share > 1
14 |
--------------------------------------------------------------------------------
/integration_tests/tests/attributed_conversions/test_scenario_1_first_touch.sql:
--------------------------------------------------------------------------------
1 | select * from {{ ref('tasman_mta__attributed_conversions') }}
2 |
3 | where
4 | --filter criteria
5 | conversion_event_id = '739982bb-f169-40d0-814d-ff633db562c0'
6 | and model_id = 'first_touch'
7 | and conversion_share = 1
8 |
9 | --success criteria
10 | and not (
11 | touch_event_id = '3f6931bc-785c-46fa-b868-6cac0bac549e'
12 | or touch_user_id = 'user1@tasman.ai'
13 | or convert_touch_count = 5
14 | or convert_seq_up = 1
15 | or convert_seq_down = 5
16 | or conversion_category = 'purchase'
17 | or touch_category = 'all_channels'
18 | )
19 |
--------------------------------------------------------------------------------
/integration_tests/tests/attributed_conversions/test_scenario_2_first_touch_7_day.sql:
--------------------------------------------------------------------------------
1 | select * from {{ ref('tasman_mta__attributed_conversions') }}
2 |
3 | where
4 | --filter criteria
5 | conversion_event_id = '739982bb-f169-40d0-814d-ff633db562c0'
6 | and model_id = 'first_touch_7_day'
7 | and conversion_share = 1
8 |
9 | --success criteria
10 | and not (
11 | touch_event_id = 'cb3a11ab-61ae-4e09-a1c6-8a49c44aa6ae'
12 | or touch_user_id = 'user1@tasman.ai'
13 | or convert_touch_count = 3
14 | or convert_seq_up = 1
15 | or convert_seq_down = 3
16 | or conversion_category = 'purchase'
17 | or touch_category = 'all_channels'
18 | )
19 |
--------------------------------------------------------------------------------
/macros/.gitkeep:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TasmanAnalytics/tasman-dbt-mta/78f49cd8c4fccdf696506aec49af99b71f232bd4/macros/.gitkeep
--------------------------------------------------------------------------------
/macros/current_utc_time.sql:
--------------------------------------------------------------------------------
1 | {% macro current_utc_time() -%}
2 | {{ return(adapter.dispatch('current_utc_time', 'tasman_dbt_mta')()) }}
3 | {%- endmacro %}
4 |
5 | {% macro snowflake__current_utc_time() %}
6 | convert_timezone('UTC', current_timestamp)::timestamp_ntz
7 | {% endmacro %}
8 |
9 | {% macro bigquery__current_utc_time() %}
10 | current_timestamp()
11 | {% endmacro %}
--------------------------------------------------------------------------------
/macros/generate_surrogate_key.sql:
--------------------------------------------------------------------------------
1 | {%- macro generate_surrogate_key(field_list) -%}
2 | {{ return(adapter.dispatch('generate_surrogate_key', 'tasman_dbt_mta')(field_list)) }}
3 | {% endmacro %}
4 |
5 | {%- macro default__generate_surrogate_key(field_list) -%}
6 |
7 | {%- if var('surrogate_key_treat_nulls_as_empty_strings', False) -%}
8 | {%- set default_null_value = "" -%}
9 | {%- else -%}
10 | {%- set default_null_value = '_dbt_utils_surrogate_key_null_' -%}
11 | {%- endif -%}
12 |
13 | {%- set fields = [] -%}
14 |
15 | {%- for field in field_list -%}
16 |
17 | {%- do fields.append(
18 | "coalesce(cast(" ~ field ~ " as " ~ dbt.type_string() ~ "), '" ~ default_null_value ~"')"
19 | ) -%}
20 |
21 | {%- if not loop.last %}
22 | {%- do fields.append("'-'") -%}
23 | {%- endif -%}
24 |
25 | {%- endfor -%}
26 |
27 | {{ dbt.hash(dbt.concat(fields)) }}
28 |
29 | {%- endmacro -%}
--------------------------------------------------------------------------------
/macros/generate_uuid.sql:
--------------------------------------------------------------------------------
1 | {% macro generate_uuid() -%}
2 | {{ return(adapter.dispatch('generate_uuid', 'tasman_dbt_mta')()) }}
3 | {%- endmacro %}
4 |
5 | {% macro snowflake__generate_uuid() %}
6 | uuid_string()
7 | {% endmacro %}
8 |
9 | {% macro bigquery__generate_uuid() %}
10 | generate_uuid()
11 | {% endmacro %}
--------------------------------------------------------------------------------
/macros/get_warehouse.sql:
--------------------------------------------------------------------------------
1 | {% macro get_warehouse() %}
2 | {% if target.name == 'prod' and var('snowflake_prod_warehouse') != '' %}
3 | {{ var('snowflake_prod_warehouse') }}
4 | {% elif target.name == 'dev' and var('snowflake_dev_warehouse') != '' %}
5 | {{ var('snowflake_dev_warehouse') }}
6 | {% else %}
7 | {{ target.warehouse }}
8 | {% endif %}
9 | {% endmacro %}
--------------------------------------------------------------------------------
/macros/test_greater_than_zero.sql:
--------------------------------------------------------------------------------
1 | {% test greater_than_zero(model, column_name) %}
2 |
3 | select *
4 | from {{ model }}
5 | where ({{ column_name }} < 0 and {{ column_name }} is not null)
6 |
7 | {% endtest %}
--------------------------------------------------------------------------------
/macros/test_in_past.sql:
--------------------------------------------------------------------------------
1 | {% test in_past(model, column_name) %}
2 |
3 | select *
4 | from {{ model }}
5 | where (cast({{ column_name }} as timestamp) >= {{ current_utc_time() }} and {{ column_name }} is not null)
6 |
7 | {% endtest %}
--------------------------------------------------------------------------------
/models/schema.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 |
3 | models:
4 | - name: tasman_mta__attributed_conversions
5 | description: "This model contains both attributed and unattributed conversion events, along with the metadata describing the attribution. There is at least 1 row per conversion per model."
6 | columns:
7 | - name: surrogate_key
8 | description: "A unique key for the table, generated from the hash of the model ID, touch event ID and conversion event ID"
9 | tests:
10 | - not_null
11 | - unique
12 |
13 | - name: conversion_user_id
14 | description: "Identifier for the user associated with the conversion"
15 | tests:
16 | - not_null
17 |
18 | - name: conversion_event_id
19 | description: "Identifer that defines a unique conversion event."
20 | tests:
21 | - not_null
22 |
23 | - name: conversion_timestamp
24 | description: "The timestamp when the conversion event happened."
25 | tests:
26 | - not_null
27 | - in_past
28 |
29 | - name: model_id
30 | description: "Identifer that specifies the attribution model that the attribution relates to."
31 | tests:
32 | - not_null
33 |
34 | - name: conversion_category
35 | description: "The category of conversion event, as defined in the conversion rules seed."
36 | tests:
37 | - not_null
38 |
39 | - name: touch_event_id
40 | description: "Identifier that defines a unique touch event."
41 |
42 | - name: touch_timestamp
43 | description: "The timestamp when the touch event happened."
44 | tests:
45 | - in_past
46 |
47 | - name: touch_user_id
48 | description: "Identifier for the user associated with the touch"
49 |
50 | - name: touch_category
51 | description: "The category of the touch, as defined in the touch rules seed."
52 |
53 | - name: convert_touch_count
54 | description: "The number of touches that have been attributed to the the conversion event."
55 | tests:
56 | - greater_than_zero
57 |
58 | - name: convert_seq_up
59 | description: "The consecutive touch number based on the timestamp ascending."
60 | tests:
61 | - greater_than_zero
62 |
63 | - name: convert_seq_down
64 | description: "The consecutive touch number based on the timestamp descending."
65 | tests:
66 | - greater_than_zero
67 |
68 | - name: interval_pre
69 | description: "Time in seconds between the touch and the touch preceding."
70 | tests:
71 | - greater_than_zero
72 |
73 | - name: interval_post
74 | description: "Time in seconds between the touch and the touch following."
75 | tests:
76 | - greater_than_zero
77 |
78 | - name: interval_convert
79 | description: "Time in seconds between the touch and the attributed conversion."
80 | tests:
81 | - greater_than_zero
82 |
83 | - name: spec
84 | description: "The attribution spec that matched this conversion, as defined in the attribution rules seed."
85 |
86 | - name: conversion_share
87 | description: "The share of the conversion attributed to this touch, expressed as a decimal percentage."
88 |
89 |
90 | - name: tasman_mta__attributed_touches
91 | description: "This model contains all touches that have been successfully attributed to a conversion. There is at least 1 row per conversion per model"
92 | columns:
93 | - name: surrogate_key
94 | description: "A unique key for the table, generated from the hash of the model ID and touch event ID"
95 | tests:
96 | - not_null
97 | - unique
98 |
99 | - name: touch_user_id
100 | description: "Identifier for the user associated with the touch"
101 | tests:
102 | - not_null
103 |
104 | - name: touch_event_id
105 | description: "Identifier that defines a unique touch event."
106 | tests:
107 | - not_null
108 |
109 | - name: touch_timestamp
110 | description: "The timestamp when the touch event happened."
111 | tests:
112 | - not_null
113 | - in_past
114 |
115 | - name: model_id
116 | description: "Identifer that specifies the attribution model that the attribution relates to."
117 | tests:
118 | - not_null
119 |
120 | - name: touch_category
121 | description: "The category of the touch, as defined in the touch rules seed."
122 | tests:
123 | - not_null
124 |
125 | - name: conversion_category
126 | description: "The category of conversion event, as defined in the conversion rules seed."
127 | tests:
128 | - not_null
129 |
130 | - name: conversion_event_id
131 | description: "Identifer that defines a unique conversion event."
132 | tests:
133 | - not_null
134 |
135 | - name: conversion_timestamp
136 | description: "The timestamp when the conversion event happened."
137 | tests:
138 | - not_null
139 | - in_past
140 |
141 | - name: att_window
142 | description: "Indentifier for the attribution window, as specific in the attribution windows seed."
143 |
144 | - name: interval_pre
145 | description: "Time in seconds between the touch and the touch preceding."
146 |
147 | - name: interval_post
148 | description: "Time in seconds between the touch and the touch following."
149 |
150 | - name: interval_convert
151 | description: "Time in seconds between the touch and the attributed conversion."
152 | tests:
153 | - not_null
154 | - greater_than_zero
155 |
156 | - name: convert_touch_count
157 | description: "The number of touches that have been attributed to the the conversion event."
158 | tests:
159 | - greater_than_zero
160 |
161 | - name: convert_seq_up
162 | description: "The consecutive touch number based on the timestamp ascending."
163 | tests:
164 | - greater_than_zero
165 |
166 | - name: convert_seq_down
167 | description: "The consecutive touch number based on the timestamp descending."
168 | tests:
169 | - greater_than_zero
170 |
171 | - name: spec
172 | description: "The attribution spec that matched this conversion, as defined in the attribution rules seed."
173 |
174 | - name: conversion_share
175 | description: "The share of the conversion attributed to this touch, expressed as a decimal percentage."
176 | tests:
177 | - greater_than_zero
178 |
179 | - name: tasman_mta__filtered_conversion_events
180 | description: "This model applies the conversion rules seed to the model defined in the 'conversions_model' variable with the project file."
181 | columns:
182 | - name: surrogate_key
183 | description: "A unique key for the table, generated from the hash of the model ID and conversion event ID"
184 | tests:
185 | - not_null:
186 | config:
187 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
188 | - unique:
189 | config:
190 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
191 |
192 | - name: conversion_user_id
193 | description: "Identifier for the user associated with the conversion"
194 | tests:
195 | - not_null:
196 | config:
197 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
198 |
199 | - name: conversion_event_id
200 | description: "Identifer that defines a unique conversion event."
201 | tests:
202 | - not_null:
203 | config:
204 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
205 |
206 | - name: conversion_timestamp
207 | description: "The timestamp when the conversion event happened."
208 | tests:
209 | - not_null:
210 | config:
211 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
212 | - in_past:
213 | config:
214 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
215 |
216 | - name: model_id
217 | description: "Identifer that specifies the attribution model that the attribution relates to."
218 | tests:
219 | - not_null:
220 | config:
221 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
222 |
223 | - name: conversion_category
224 | description: "The category of conversion event, as defined in the conversion rules seed."
225 | tests:
226 | - not_null:
227 | config:
228 | where: "conversion_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
229 |
230 | - name: tasman_mta__filtered_touch_events
231 | description: "This model applies the touch rules seed to the model defined in the 'touches_model' variable with the project file."
232 | columns:
233 | - name: surrogate_key
234 | description: "A unique key for the table, generated from the hash of the model ID and touch event ID"
235 | tests:
236 | - not_null:
237 | config:
238 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
239 | - unique:
240 | config:
241 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
242 |
243 | - name: touch_user_id
244 | description: "Identifier for the user associated with the touch"
245 | tests:
246 | - not_null:
247 | config:
248 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
249 |
250 | - name: touch_event_id
251 | description: "Identifer that defines a unique touch event."
252 | tests:
253 | - not_null:
254 | config:
255 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
256 |
257 | - name: touch_timestamp
258 | description: "The timestamp when the touch event happened."
259 | tests:
260 | - not_null:
261 | config:
262 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
263 | - in_past:
264 | config:
265 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
266 |
267 | - name: model_id
268 | description: "Identifer that specifies the attribution model that the attribution relates to."
269 | tests:
270 | - not_null:
271 | config:
272 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
273 |
274 | - name: touch_category
275 | description: "The category of the touch, as defined in the touch rules seed."
276 | tests:
277 | - not_null:
278 | config:
279 | where: "touch_timestamp >= DATEADD(HOUR, -{{ var('test_hours') }}, current_timestamp())"
--------------------------------------------------------------------------------
/models/tasman_mta__attributed_conversions.sql:
--------------------------------------------------------------------------------
1 |
2 | {{
3 | config(
4 | materialized='table',
5 | snowflake_warehouse=get_warehouse()
6 | )
7 | }}
8 |
9 | with
10 |
11 | conversion_events as (
12 | select * from {{ ref('tasman_mta__filtered_conversion_events') }}
13 | ),
14 |
15 | attributed_touches as (
16 | select * from {{ ref('tasman_mta__attributed_touches') }}
17 | ),
18 |
19 | joined_conversion_events as (
20 |
21 | select
22 | {{ generate_surrogate_key([
23 | 'attributed_touches.model_id',
24 | 'conversion_events.model_id',
25 | 'attributed_touches.touch_event_id',
26 | 'conversion_events.conversion_event_id'
27 | ]) }} as surrogate_key,
28 | conversion_events.conversion_user_id,
29 | conversion_events.conversion_event_id,
30 | conversion_events.conversion_timestamp,
31 | conversion_events.model_id,
32 | conversion_events.conversion_category,
33 | attributed_touches.touch_event_id,
34 | attributed_touches.touch_timestamp,
35 | attributed_touches.touch_user_id,
36 | attributed_touches.touch_category,
37 | attributed_touches.att_window,
38 | attributed_touches.convert_touch_count,
39 | attributed_touches.convert_seq_up,
40 | attributed_touches.convert_seq_down,
41 | attributed_touches.interval_pre,
42 | attributed_touches.interval_post,
43 | attributed_touches.interval_convert,
44 | attributed_touches.spec,
45 | attributed_touches.conversion_share
46 |
47 | from
48 | conversion_events
49 |
50 | left join
51 | attributed_touches
52 | on conversion_events.conversion_event_id = attributed_touches.conversion_event_id
53 | and conversion_events.model_id = attributed_touches.model_id
54 | )
55 |
56 | select * from joined_conversion_events
57 |
--------------------------------------------------------------------------------
/models/tasman_mta__attributed_touches.sql:
--------------------------------------------------------------------------------
1 | {{
2 | config(
3 | materialized='table',
4 | snowflake_warehouse=get_warehouse()
5 | )
6 | }}
7 |
8 | with
9 |
10 | touches as (
11 | select * from {{ ref('tasman_mta__filtered_touch_events') }}
12 | ),
13 |
14 | conversions as (
15 | select * from {{ ref('tasman_mta__filtered_conversion_events') }}
16 | ),
17 |
18 | attribution_rules as (
19 | select * from {{ var('attribution_rules') }}
20 | ),
21 |
22 | conversion_shares as (
23 | select * from {{ var('conversion_shares') }}
24 | ),
25 |
26 | attribution_windows as (
27 | select * from {{ var('attribution_windows') }}
28 | ),
29 |
30 | conversions_after_touches as (
31 |
32 | select
33 | touches.touch_user_id,
34 | touches.touch_event_id,
35 | touches.touch_timestamp,
36 | touches.model_id,
37 | touches.touch_category,
38 | conversions.conversion_event_id,
39 | conversions.conversion_timestamp,
40 | conversions.conversion_category
41 |
42 | from
43 | touches
44 | inner join conversions
45 | on
46 | touches.touch_user_id = conversions.conversion_user_id
47 | and touches.model_id = conversions.model_id
48 | and touches.touch_timestamp < conversions.conversion_timestamp
49 | where
50 | touches.touch_user_id is not null
51 | ),
52 |
53 | matched_touches as (
54 |
55 | select distinct
56 | touch_user_id,
57 | touch_event_id,
58 | touch_timestamp,
59 | model_id,
60 | touch_category,
61 | case
62 | when conversion_category is not null
63 | then first_value(conversion_event_id) over (partition by touch_user_id, touch_event_id, model_id, touch_category order by conversion_timestamp rows unbounded preceding)
64 | end as conversion_event_id,
65 | case
66 | when conversion_category is not null
67 | then first_value(conversion_timestamp) over (partition by touch_user_id, touch_event_id, model_id, touch_category order by conversion_timestamp rows unbounded preceding)
68 | end as conversion_timestamp,
69 | case
70 | when conversion_category is not null
71 | then first_value(conversion_category) over (partition by touch_user_id, touch_event_id, model_id, touch_category order by conversion_timestamp rows unbounded preceding)
72 | end as conversion_category
73 |
74 | from
75 | conversions_after_touches
76 | ),
77 |
78 | conversion_intervals as (
79 | select
80 | matched_touches.touch_user_id,
81 | matched_touches.touch_event_id,
82 | matched_touches.touch_timestamp,
83 | matched_touches.model_id,
84 | matched_touches.touch_category,
85 | matched_touches.conversion_category,
86 | matched_touches.conversion_event_id,
87 | matched_touches.conversion_timestamp,
88 | case
89 | when matched_touches.conversion_category is not null
90 | then {{ dbt.datediff("matched_touches.touch_timestamp", "matched_touches.conversion_timestamp", 'second') }}
91 | end as interval_convert,
92 | attribution_windows.att_window,
93 | attribution_windows.time_seconds
94 |
95 | from
96 | matched_touches
97 | inner join
98 | attribution_windows
99 | on matched_touches.model_id = attribution_windows.model_id
100 |
101 | ),
102 |
103 | windowed_touches as (
104 | select
105 | *
106 | from
107 | conversion_intervals
108 | where
109 | interval_convert < time_seconds
110 | or time_seconds = 0
111 |
112 | ),
113 |
114 | touch_events as (
115 |
116 | select
117 | touch_user_id,
118 | touch_event_id,
119 | touch_timestamp,
120 | model_id,
121 | touch_category,
122 | conversion_category,
123 | conversion_event_id,
124 | conversion_timestamp,
125 | att_window,
126 | interval_convert,
127 | case
128 | when conversion_category is not null
129 | then {{ dbt.datediff("lag(touch_timestamp) over (partition by conversion_event_id, model_id order by touch_timestamp)", "touch_timestamp", 'second') }}
130 | end as interval_pre,
131 | case
132 | when conversion_category is not null
133 | then {{ dbt.datediff("touch_timestamp", "coalesce(lead(touch_timestamp, 1) over (partition by conversion_event_id, model_id order by touch_timestamp), conversion_timestamp)", 'second') }}
134 | end as interval_post,
135 | case
136 | when conversion_category is not null
137 | then count(distinct touch_event_id) over (partition by conversion_event_id, model_id)
138 | end as convert_touch_count,
139 | case
140 | when conversion_category is not null
141 | then rank() over (partition by conversion_event_id, model_id order by touch_timestamp)
142 | end as convert_seq_up,
143 | case
144 | when conversion_category is not null
145 | then rank() over (partition by conversion_event_id, model_id order by touch_timestamp desc)
146 | end as convert_seq_down
147 |
148 |
149 | from
150 | windowed_touches
151 | ),
152 |
153 | touch_taxonomy as (
154 |
155 | select 'touch_category' as attribute union all
156 | select 'conversion_category' as attribute union all
157 | select 'convert_touch_count' as attribution union all
158 | select 'convert_seq_up' as attribute union all
159 | select 'convert_seq_down' as attribute union all
160 | select 'interval_pre' as attribute union all
161 | select 'interval_post' as attribute union all
162 | select 'interval_convert' as attribute
163 |
164 | ),
165 |
166 | touch_attributes as (
167 |
168 | select
169 | touch_events.touch_user_id,
170 | touch_events.touch_event_id,
171 | touch_events.conversion_event_id,
172 | touch_events.model_id,
173 | touch_taxonomy.attribute,
174 |
175 | case
176 | when touch_taxonomy.attribute = 'touch_category' then cast(touch_events.touch_category as string)
177 | when touch_taxonomy.attribute = 'conversion_category' then cast(touch_events.conversion_category as string)
178 | when touch_taxonomy.attribute = 'convert_seq_up' then cast(touch_events.convert_seq_up as string)
179 | when touch_taxonomy.attribute = 'convert_seq_down' then cast(touch_events.convert_seq_down as string)
180 | when touch_taxonomy.attribute = 'interval_pre' then cast(touch_events.interval_pre as string)
181 | when touch_taxonomy.attribute = 'interval_post' then cast(touch_events.interval_post as string)
182 | when touch_taxonomy.attribute = 'interval_convert' then cast(touch_events.interval_convert as string)
183 | end as value
184 |
185 | from touch_events, touch_taxonomy
186 | ),
187 |
188 | attribution_parts as (
189 |
190 | select
191 | attribution_rules.*,
192 | power(2, attribution_rules.part - 1) as bit
193 |
194 | from
195 | attribution_rules
196 | ),
197 |
198 | rules_bitsums as (
199 |
200 | select
201 | model_id,
202 | spec,
203 | rule,
204 | power(2, max(part)) - 1 as bitsum
205 |
206 | from
207 | attribution_parts
208 |
209 | group by
210 | model_id,
211 | spec,
212 | rule
213 |
214 | order by
215 | model_id,
216 | spec,
217 | rule
218 | ),
219 |
220 | matched_parts as (
221 |
222 | select
223 | touch_attributes.touch_user_id,
224 | touch_attributes.touch_event_id,
225 | touch_attributes.conversion_event_id,
226 | touch_attributes.attribute,
227 | touch_attributes.value,
228 | attribution_parts.model_id,
229 | attribution_parts.spec,
230 | attribution_parts.rule,
231 | attribution_parts.part,
232 | attribution_parts.bit
233 |
234 | from
235 | touch_attributes
236 | inner join attribution_parts on
237 | touch_attributes.attribute = attribution_parts.attribute
238 | and touch_attributes.model_id = attribution_parts.model_id
239 |
240 | where
241 | (attribution_parts.relation = '=' and touch_attributes.value = cast(attribution_parts.value as string))
242 | or (attribution_parts.relation = '>=' and touch_attributes.value >= cast(attribution_parts.value as string))
243 | or (attribution_parts.relation = '<=' and touch_attributes.value <= cast(attribution_parts.value as string))
244 | or (attribution_parts.relation = '>' and touch_attributes.value > cast(attribution_parts.value as string))
245 | or (attribution_parts.relation = '<' and touch_attributes.value < cast(attribution_parts.value as string))
246 | or (attribution_parts.relation = '<>' and touch_attributes.value <> cast(attribution_parts.value as string))
247 | ),
248 |
249 | matched_rules as (
250 |
251 | select
252 | matched_parts.touch_user_id,
253 | matched_parts.touch_event_id,
254 | matched_parts.conversion_event_id,
255 | matched_parts.model_id,
256 | matched_parts.spec,
257 | matched_parts.rule,
258 | sum(matched_parts.bit) as bits,
259 | rules_bitsums.bitsum
260 |
261 | from
262 | matched_parts
263 | inner join rules_bitsums on
264 | matched_parts.model_id = rules_bitsums.model_id
265 | and matched_parts.spec = rules_bitsums.spec
266 | and matched_parts.rule = rules_bitsums.rule
267 |
268 | group by
269 | matched_parts.touch_user_id,
270 | matched_parts.touch_event_id,
271 | matched_parts.conversion_event_id,
272 | matched_parts.model_id,
273 | matched_parts.spec,
274 | matched_parts.rule,
275 | rules_bitsums.bitsum
276 |
277 | having
278 | bits = rules_bitsums.bitsum
279 | ),
280 |
281 | matched_groups as (
282 |
283 | select distinct
284 | touch_user_id,
285 | touch_event_id,
286 | conversion_event_id,
287 | model_id,
288 | spec
289 |
290 | from
291 | matched_rules
292 | ),
293 |
294 | share_attribution as (
295 |
296 | select
297 | matched_groups.touch_user_id,
298 | matched_groups.touch_event_id,
299 | matched_groups.model_id,
300 | matched_groups.spec,
301 | conversion_shares.share / count(matched_groups.touch_event_id) over (partition by matched_groups.touch_user_id, matched_groups.model_id, matched_groups.spec) as conversion_share
302 |
303 | from
304 | matched_groups
305 | inner join conversion_shares on
306 | matched_groups.model_id = conversion_shares.model_id
307 | and matched_groups.spec = conversion_shares.spec
308 | ),
309 |
310 | attributed_events as (
311 | select
312 | {{ generate_surrogate_key([
313 | 'touch_events.model_id',
314 | 'touch_events.touch_event_id'
315 | ]) }} as surrogate_key,
316 | touch_events.*,
317 | share_attribution.spec,
318 | share_attribution.conversion_share
319 |
320 | from
321 | touch_events
322 | left join share_attribution on
323 | touch_events.touch_event_id = share_attribution.touch_event_id
324 | and touch_events.model_id = share_attribution.model_id
325 | )
326 |
327 |
328 | select * from attributed_events
--------------------------------------------------------------------------------
/models/tasman_mta__filtered_conversion_events.sql:
--------------------------------------------------------------------------------
1 | {% if var('incremental') == 'true' %}
2 | {{
3 | config(
4 | materialized='incremental',
5 | snowflake_warehouse=get_warehouse()
6 | )
7 | }}
8 | {% else %}
9 | {{
10 | config(
11 | materialized='table',
12 | snowflake_warehouse=get_warehouse()
13 | )
14 | }}
15 | {% endif %}
16 |
17 | with
18 |
19 | conversion_events as (
20 | select * from {{ var('conversions_model') }}
21 | {% if is_incremental() %}
22 | -- this filter will only be applied on an incremental run
23 | where
24 | {{var('conversions_timestamp_field')}} > (select max(conversion_timestamp) from {{ this }})
25 | {% endif %}
26 | ),
27 |
28 | conversion_rules as (
29 | select * from {{ var('conversion_rules') }}
30 | ),
31 |
32 | conversion_attributes as (
33 | select distinct attribute from conversion_rules
34 | ),
35 |
36 | {%- set attributes_query -%}
37 | select distinct attribute from {{ var('conversion_rules') }}
38 | {%- endset -%}
39 |
40 | {%- if execute -%}
41 | {% set attributes = run_query(attributes_query).rows %}
42 | {%- else -%}
43 | {% set attributes = [] %}
44 | {%- endif -%}
45 |
46 | raw_event_attributes as (
47 | select
48 | conversion_events.{{var('conversions_event_id_field')}} as conversion_event_id,
49 | conversion_events.{{var('conversions_timestamp_field')}} as conversion_timestamp,
50 | conversion_events.{{var('conversions_user_id_field')}} as conversion_user_id,
51 | conversion_attributes.attribute as attribute,
52 | case
53 | {% for attribute in attributes -%}
54 | when conversion_attributes.attribute = '{{ attribute[0] }}' then cast({{ attribute[0] }} as string)
55 | {% endfor %}
56 | end as value
57 |
58 | from
59 | conversion_events, conversion_attributes
60 |
61 | ),
62 |
63 | event_attributes as (
64 | select
65 | *
66 | from
67 | raw_event_attributes
68 | where
69 | value is not null
70 |
71 | ),
72 |
73 | conversion_rules_bit as ( -- maps rule parts to their attribute types and adds a bit used for validating that all parts of any rule are matched
74 | select
75 | *,
76 | power(2, part - 1) as bit
77 |
78 | from
79 | conversion_rules
80 |
81 | ),
82 |
83 | conversion_rules_compiled as ( -- converts the value of the rule part predicate to the appropriate native type, for better performance
84 |
85 | select
86 | *,
87 | case
88 | when type = 'boolean' and value = 'true' then true
89 | when type = 'boolean' and value = 'false' then false
90 | else null
91 | end as boolean_value,
92 | case
93 | when type = 'integer' then {{dbt.safe_cast("value", "integer")}}
94 | else null
95 | end as integer_value,
96 | case
97 | when type = 'float' then {{dbt.safe_cast("value", "numeric")}}
98 | else null
99 | end as float_value
100 |
101 | from
102 | conversion_rules_bit
103 | ),
104 |
105 | rules_bitsums as ( --calculates the sum of the of the bits per rule needed to validate that all parts per rule are satisfied.
106 |
107 | select
108 | model_id,
109 | conversion_category,
110 | rule,
111 | power(2, max(part)) - 1 as bitsum
112 |
113 | from
114 | conversion_rules_bit
115 |
116 | group by
117 | model_id,
118 | conversion_category,
119 | rule
120 |
121 | order by
122 | model_id,
123 | conversion_category,
124 | rule
125 | ),
126 |
127 | matched_parts as ( --returns all matched parts of the rules from the event stream (contains duplicates, handled downstreams.)
128 |
129 | select
130 | event_attributes.conversion_user_id,
131 | event_attributes.conversion_event_id,
132 | event_attributes.conversion_timestamp,
133 | event_attributes.attribute,
134 | event_attributes.value,
135 | rules.model_id,
136 | rules.conversion_category,
137 | rules.rule,
138 | rules.part,
139 | rules.bit
140 |
141 | from
142 | event_attributes
143 | inner join conversion_rules_compiled as rules on
144 | event_attributes.attribute = rules.attribute
145 |
146 | where
147 | (rules.type = 'boolean'
148 | and (
149 | (rules.relation = '=' and event_attributes.value = 'true' and rules.boolean_value = true)
150 | or (rules.relation = '=' and event_attributes.value = 'false' and rules.boolean_value = false)
151 | )
152 | )
153 |
154 | or (rules.type = 'integer'
155 | and (
156 | (rules.relation = '=' and {{dbt.safe_cast("event_attributes.value", "integer")}} = rules.integer_value)
157 | or (rules.relation = '>=' and {{dbt.safe_cast("event_attributes.value", "integer")}} >= rules.integer_value)
158 | or (rules.relation = '<=' and {{dbt.safe_cast("event_attributes.value", "integer")}} <= rules.integer_value)
159 | or (rules.relation = '>' and {{dbt.safe_cast("event_attributes.value", "integer")}} > rules.integer_value)
160 | or (rules.relation = '<' and {{dbt.safe_cast("event_attributes.value", "integer")}} < rules.integer_value)
161 | or (rules.relation = '<>' and {{dbt.safe_cast("event_attributes.value", "integer")}} <> rules.integer_value)
162 | )
163 | )
164 |
165 | or (rules.type = 'float'
166 | and (
167 | (rules.relation = '=' and {{dbt.safe_cast("event_attributes.value", "numeric")}} = rules.float_value)
168 | or (rules.relation = '>=' and {{dbt.safe_cast("event_attributes.value", "numeric")}} >= rules.float_value)
169 | or (rules.relation = '<=' and {{dbt.safe_cast("event_attributes.value", "numeric")}} <= rules.float_value)
170 | or (rules.relation = '>' and {{dbt.safe_cast("event_attributes.value", "numeric")}} > rules.float_value)
171 | or (rules.relation = '<' and {{dbt.safe_cast("event_attributes.value", "numeric")}} < rules.float_value)
172 | )
173 | )
174 |
175 | or (rules.type = 'string'
176 | and (
177 | (rules.relation = '=' and event_attributes.value = rules.value)
178 | or (rules.relation = '>=' and event_attributes.value >= rules.value)
179 | or (rules.relation = '<=' and event_attributes.value <= rules.value)
180 | or (rules.relation = '>' and event_attributes.value > rules.value)
181 | or (rules.relation = '<' and event_attributes.value < rules.value)
182 | or (rules.relation = '<>' and event_attributes.value <> rules.value)
183 | )
184 | )
185 | ),
186 |
187 | matched_rules as ( -- returns fulfilled rules which indicates that an event matches a conversion category
188 |
189 | select
190 | matched_parts.conversion_user_id,
191 | matched_parts.conversion_event_id,
192 | matched_parts.conversion_timestamp,
193 | matched_parts.model_id,
194 | matched_parts.conversion_category,
195 | matched_parts.rule,
196 | sum(matched_parts.bit) as bits,
197 | rules_bitsums.bitsum
198 |
199 | from
200 | matched_parts
201 | inner join rules_bitsums on
202 | rules_bitsums.model_id = matched_parts.model_id
203 | and rules_bitsums.conversion_category = matched_parts.conversion_category
204 | and rules_bitsums.rule = matched_parts.rule
205 |
206 | group by
207 | matched_parts.conversion_user_id,
208 | matched_parts.conversion_event_id,
209 | matched_parts.conversion_timestamp,
210 | matched_parts.model_id,
211 | matched_parts.conversion_category,
212 | matched_parts.rule,
213 | rules_bitsums.bitsum
214 |
215 | having
216 | bits = rules_bitsums.bitsum
217 | ),
218 |
219 | matched_categories as (-- Return one event record per conversion category (for the case where an event matches multiple rules within a conversion category)
220 |
221 | select distinct
222 | {{ generate_surrogate_key([
223 | 'model_id',
224 | 'conversion_event_id'
225 | ]) }} as surrogate_key,
226 | conversion_user_id,
227 | conversion_event_id,
228 | conversion_timestamp,
229 | model_id,
230 | conversion_category
231 |
232 | from
233 | matched_rules
234 | )
235 |
236 | select * from matched_categories
237 |
--------------------------------------------------------------------------------
/models/tasman_mta__filtered_touch_events.sql:
--------------------------------------------------------------------------------
1 | {% if var('incremental') == 'true' %}
2 | {{config(materialized='incremental', snowflake_warehouse=get_warehouse())}}
3 | {% else %}
4 | {{config(materialized='table', snowflake_warehouse=get_warehouse())}}
5 | {% endif %}
6 |
7 | with
8 |
9 | touch_events as (
10 | select * from {{ var('touches_model') }}
11 | {% if is_incremental() %}
12 | -- this filter will only be applied on an incremental run
13 | where
14 | {{var('touches_timestamp_field')}} > (select max(touch_timestamp) from {{ this }})
15 | {% endif %}
16 | ),
17 |
18 | touch_rules as (
19 | select * from {{ var('touch_rules') }}
20 | ),
21 |
22 | touch_attributes as (
23 | select distinct attribute from touch_rules
24 | ),
25 |
26 | {%- set attributes_query -%}
27 | select distinct attribute from {{ var('touch_rules') }}
28 | {%- endset -%}
29 |
30 | {%- if execute -%}
31 | {% set attributes = run_query(attributes_query).rows %}
32 | {%- else -%}
33 | {% set attributes = [] %}
34 | {%- endif -%}
35 |
36 | raw_event_attributes as (
37 | select
38 | touch_events.{{var('touches_event_id_field')}} as touch_event_id,
39 | touch_events.{{var('touches_timestamp_field')}} as touch_timestamp,
40 | touch_events.{{var('touches_user_id_field')}} as touch_user_id,
41 | touch_attributes.attribute as attribute,
42 | case
43 | {% for attribute in attributes -%}
44 | when touch_attributes.attribute = '{{ attribute[0] }}' then cast({{ attribute[0] }} as string)
45 | {% endfor %}
46 | end as value
47 |
48 | from
49 | touch_events, touch_attributes
50 |
51 | ),
52 |
53 | event_attributes as (
54 | select
55 | *
56 | from
57 | raw_event_attributes
58 | where
59 | value is not null
60 | ),
61 |
62 | touch_rules_bit as ( -- maps rule parts to their attribute types and adds a bit used for validating that all parts of any rule are matched
63 |
64 | select
65 | *,
66 | power(2, part - 1) as bit
67 |
68 | from
69 | touch_rules
70 | ),
71 |
72 | touch_rules_compiled as ( -- converts the value of the rule part predicate to the appropriate native type, for better performance
73 |
74 | select
75 | *,
76 | case
77 | when type = 'boolean' and value = 'true' then true
78 | when type = 'boolean' and value = 'false' then false
79 | else null
80 | end as boolean_value,
81 | case
82 | when type = 'integer' then {{dbt.safe_cast("value", "integer")}}
83 | else null
84 | end as integer_value,
85 | case
86 | when type = 'float' then {{dbt.safe_cast("value", "numeric")}}
87 | else null
88 | end as float_value
89 |
90 | from
91 | touch_rules_bit
92 | ),
93 |
94 | rules_bitsums as ( --calculates the sum of the of the bits per rule needed to validate that all parts per rule are satisfied.
95 |
96 | select
97 | model_id,
98 | touch_category,
99 | rule,
100 | power(2, max(part)) - 1 as bitsum
101 |
102 | from
103 | touch_rules_bit
104 |
105 | group by
106 | model_id,
107 | touch_category,
108 | rule
109 |
110 | order by
111 | model_id,
112 | touch_category,
113 | rule
114 | ),
115 |
116 | matched_parts as ( --returns all matched parts of the rules from the event stream (contains duplicates, handled downstreams.)
117 |
118 | select
119 | event_attributes.touch_user_id,
120 | event_attributes.touch_event_id,
121 | event_attributes.touch_timestamp,
122 | event_attributes.attribute,
123 | event_attributes.value,
124 | rules.model_id,
125 | rules.touch_category,
126 | rules.rule,
127 | rules.part,
128 | rules.bit
129 |
130 | from
131 | event_attributes
132 | inner join touch_rules_compiled as rules on
133 | event_attributes.attribute = rules.attribute
134 |
135 | where
136 | (rules.type = 'boolean'
137 | and (
138 | (rules.relation = '=' and event_attributes.value = 'true' and rules.boolean_value = true)
139 | or (rules.relation = '=' and event_attributes.value = 'false' and rules.boolean_value = false)
140 | )
141 | )
142 |
143 | or (rules.type = 'integer'
144 | and (
145 | (rules.relation = '=' and {{dbt.safe_cast("event_attributes.value", "integer")}} = rules.integer_value)
146 | or (rules.relation = '>=' and {{dbt.safe_cast("event_attributes.value", "integer")}} >= rules.integer_value)
147 | or (rules.relation = '<=' and {{dbt.safe_cast("event_attributes.value", "integer")}} <= rules.integer_value)
148 | or (rules.relation = '>' and {{dbt.safe_cast("event_attributes.value", "integer")}} > rules.integer_value)
149 | or (rules.relation = '<' and {{dbt.safe_cast("event_attributes.value", "integer")}} < rules.integer_value)
150 | or (rules.relation = '<>' and {{dbt.safe_cast("event_attributes.value", "integer")}} <> rules.integer_value)
151 | )
152 | )
153 |
154 | or (rules.type = 'float'
155 | and (
156 | (rules.relation = '=' and {{dbt.safe_cast("event_attributes.value", "numeric")}} = rules.float_value)
157 | or (rules.relation = '>=' and {{dbt.safe_cast("event_attributes.value", "numeric")}} >= rules.float_value)
158 | or (rules.relation = '<=' and {{dbt.safe_cast("event_attributes.value", "numeric")}} <= rules.float_value)
159 | or (rules.relation = '>' and {{dbt.safe_cast("event_attributes.value", "numeric")}} > rules.float_value)
160 | or (rules.relation = '<' and {{dbt.safe_cast("event_attributes.value", "numeric")}} < rules.float_value)
161 | )
162 | )
163 |
164 | or (rules.type = 'string'
165 | and (
166 | (rules.relation = '=' and event_attributes.value = rules.value)
167 | or (rules.relation = '>=' and event_attributes.value >= rules.value)
168 | or (rules.relation = '<=' and event_attributes.value <= rules.value)
169 | or (rules.relation = '>' and event_attributes.value > rules.value)
170 | or (rules.relation = '<' and event_attributes.value < rules.value)
171 | or (rules.relation = '<>' and event_attributes.value <> rules.value)
172 | )
173 | )
174 | ),
175 |
176 | matched_rules as ( -- returns fulfilled rules which indicates that an event matches a touch touch_category
177 |
178 | select
179 | matched_parts.touch_user_id,
180 | matched_parts.touch_event_id,
181 | matched_parts.touch_timestamp,
182 | matched_parts.model_id,
183 | matched_parts.touch_category,
184 | matched_parts.rule,
185 | sum(matched_parts.bit) as bits,
186 | rules_bitsums.bitsum
187 |
188 | from
189 | matched_parts
190 | inner join rules_bitsums on
191 | rules_bitsums.model_id = matched_parts.model_id
192 | and rules_bitsums.touch_category = matched_parts.touch_category
193 | and rules_bitsums.rule = matched_parts.rule
194 |
195 | group by
196 | matched_parts.touch_user_id,
197 | matched_parts.touch_event_id,
198 | matched_parts.touch_timestamp,
199 | matched_parts.model_id,
200 | matched_parts.touch_category,
201 | matched_parts.rule,
202 | rules_bitsums.bitsum
203 |
204 | having
205 | bits = rules_bitsums.bitsum
206 | ),
207 |
208 | matched_categories as (-- Return one event record per touch_category (for the case where an event matches multiple rules within a touch_category)
209 |
210 | select distinct
211 | {{ generate_surrogate_key([
212 | 'model_id',
213 | 'touch_event_id'
214 | ]) }} as surrogate_key,
215 | touch_user_id,
216 | touch_event_id,
217 | touch_timestamp,
218 | model_id,
219 | touch_category
220 |
221 | from
222 | matched_rules
223 | )
224 |
225 | select * from matched_categories
226 |
--------------------------------------------------------------------------------
/models/tasman_mta__performance_history.sql:
--------------------------------------------------------------------------------
1 | {{
2 | config(
3 | materialized='incremental',
4 | on_schema_change="sync_all_columns",
5 | snowflake_warehouse=get_warehouse(),
6 | full_refresh=false
7 | )
8 | }}
9 |
10 | with
11 |
12 | models as (
13 | select model_id from {{var('touch_rules')}}
14 | union all
15 | select model_id from {{var('conversion_rules')}}
16 | union all
17 | select model_id from {{var('conversion_shares')}}
18 | union all
19 | select model_id from {{var('attribution_rules')}}
20 | union all
21 | select model_id from {{var('attribution_windows')}}
22 | ),
23 |
24 | distinct_models as (
25 | select distinct model_id from models
26 | ),
27 |
28 | input_touches as (
29 | select
30 | count(distinct {{var('touches_event_id_field')}}) as input_touches
31 | from
32 | {{ var('touches_model') }}
33 | ),
34 |
35 | input_touches_by_model as (
36 | select
37 | distinct_models.model_id,
38 | input_touches.input_touches
39 |
40 | from
41 | input_touches, distinct_models
42 | ),
43 |
44 | filtered_touches_by_model as (
45 | select
46 | model_id,
47 | count(distinct touch_event_id) as filtered_touches
48 | from
49 | {{ ref('tasman_mta__filtered_touch_events') }}
50 | group by
51 | model_id
52 | ),
53 |
54 | attributed_touches_by_model as (
55 | select
56 | model_id,
57 | count(distinct touch_event_id) as attributed_touches
58 | from
59 | {{ ref('tasman_mta__attributed_touches') }}
60 | where
61 | conversion_event_id is not null
62 | group by
63 | model_id
64 | ),
65 |
66 | input_conversions as (
67 | select
68 | count(distinct {{var('conversions_event_id_field')}}) as input_conversions
69 | from
70 | {{ var('conversions_model') }}
71 | ),
72 |
73 | input_conversions_by_model as (
74 | select
75 | distinct_models.model_id,
76 | input_conversions.input_conversions
77 |
78 | from
79 | input_conversions, distinct_models
80 | ),
81 |
82 | filtered_conversions_by_model as (
83 | select
84 | model_id,
85 | count(distinct conversion_event_id) as filtered_conversions
86 | from
87 | {{ ref('tasman_mta__filtered_conversion_events') }}
88 | group by
89 | model_id
90 | ),
91 |
92 | attributed_conversions_by_model as (
93 | select
94 | model_id,
95 | count(distinct conversion_event_id) as attributed_conversions
96 | from
97 | {{ ref('tasman_mta__attributed_conversions') }}
98 | where
99 | touch_event_id is not null
100 | group by
101 | model_id
102 | ),
103 |
104 | unattributed_conversions_by_model as (
105 | select
106 | model_id,
107 | count(distinct conversion_event_id) as unattributed_conversions
108 | from
109 | {{ ref('tasman_mta__attributed_conversions') }}
110 | where
111 | touch_event_id is null
112 | group by
113 | model_id
114 | ),
115 |
116 | conversion_share_by_model as (
117 | select
118 | model_id,
119 | sum(conversion_share) as total_conversion_share
120 | from
121 | {{ ref('tasman_mta__attributed_conversions') }}
122 | group by
123 | model_id
124 | ),
125 |
126 | run_details as (
127 | select
128 | {{ generate_uuid() }} as run_id,
129 | {{ current_utc_time() }} as run_date
130 | ),
131 |
132 | run_details_by_model as (
133 | select
134 | run_details.run_id,
135 | run_details.run_date,
136 | distinct_models.model_id
137 |
138 | from
139 | run_details, distinct_models
140 | ),
141 |
142 | joined_stats as (
143 | select
144 | run_details_by_model.run_id,
145 | run_details_by_model.run_date,
146 | run_details_by_model.model_id,
147 | input_touches_by_model.input_touches,
148 | filtered_touches_by_model.filtered_touches,
149 | attributed_touches_by_model.attributed_touches,
150 | input_conversions_by_model.input_conversions,
151 | filtered_conversions_by_model.filtered_conversions,
152 | attributed_conversions_by_model.attributed_conversions,
153 | unattributed_conversions_by_model.unattributed_conversions,
154 | conversion_share_by_model.total_conversion_share
155 |
156 | from
157 | run_details_by_model
158 |
159 | left join
160 | input_touches_by_model
161 | on run_details_by_model.model_id = input_touches_by_model.model_id
162 |
163 | left join
164 | filtered_touches_by_model
165 | on run_details_by_model.model_id = filtered_touches_by_model.model_id
166 |
167 | left join
168 | attributed_touches_by_model
169 | on run_details_by_model.model_id = attributed_touches_by_model.model_id
170 |
171 | left join
172 | input_conversions_by_model
173 | on run_details_by_model.model_id = input_conversions_by_model.model_id
174 |
175 | left join
176 | filtered_conversions_by_model
177 | on run_details_by_model.model_id = filtered_conversions_by_model.model_id
178 |
179 | left join
180 | attributed_conversions_by_model
181 | on run_details_by_model.model_id = attributed_conversions_by_model.model_id
182 |
183 | left join
184 | unattributed_conversions_by_model
185 | on run_details_by_model.model_id = unattributed_conversions_by_model.model_id
186 |
187 | left join
188 | conversion_share_by_model
189 | on run_details_by_model.model_id = conversion_share_by_model.model_id
190 |
191 | ),
192 |
193 | calculated_stats as (
194 | select
195 | run_id,
196 | run_date,
197 | model_id,
198 | input_touches,
199 | filtered_touches,
200 | input_touches - filtered_touches as removed_touches,
201 | attributed_touches,
202 | filtered_touches - attributed_touches as unattributed_touches,
203 | input_conversions,
204 | filtered_conversions,
205 | input_conversions - filtered_conversions as removed_conversions,
206 | attributed_conversions,
207 | unattributed_conversions,
208 | attributed_conversions/filtered_conversions as attribution_rate,
209 | total_conversion_share
210 |
211 | from
212 | joined_stats
213 | )
214 |
215 | select * from calculated_stats
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | [![tasman_logo][tasman_wordmark_black]][tasman_website_light_mode]
2 | [![tasman_logo][tasman_wordmark_cream]][tasman_website_dark_mode]
3 |
4 | ---
5 | *We are the boutique analytics consultancy that turns disorganised data into real business value. [Get in touch][tasman_contact] to learn more about how Tasman can help solve your organisations data challenges.*
6 |
7 | # Multi-Touch Attribution Engine
8 |
9 | **Key Features:**
10 | - 🔩 Boolean-algebra rule-based configurations avoiding custom SQL requirements
11 | - 🪛 Reconfigurable positional and time-based attribution models
12 | - 🔀 Multiple concurrent models, enabling robust, flexible, multi-model analyses
13 | - ⏰ Fine-grain attribution window control
14 | - ➕ Optional incremental materialisations
15 | - ❄️ Custom warehouse selection (Snowflake only)
16 |
17 |
18 | ## What is Multi-Touch Attribution? 🤨
19 |
20 | Multi-touch attribution is a method of marketing measurement that accounts for all the touchpoints on the customer journey and designates a certain amount of credit to each channel. This enables marketers to analyse the value that each touchpoint has on driving a conversion.
21 |
22 | The core functionality of an attribution engine is its ability to match touches to conversions based on a series of rules, known as 'attribution models'.
23 |
24 | Multi-touch attribution can be cross-device, however with the increased privacy constraints introduced by Apple in iOS 14.5 and more generally across the industry, deterministic methods of attribution such as those in this engine are generally ineffective for mobile. For mobile attribution, we recommend checking Mobile Measurement Partners (MMPs) with support for Apple's SKAdNetwork such as [Appsflyer](https://www.appsflyer.com/) or [Adjust](https://www.adjust.com/).
25 |
26 | >Examples of attribution models that can be configured with this engine include:
27 | >- Last touch - 100% conversion credit is applied to the touch point immediately before the conversion event
28 | >- First touch - 100% conversion credit is applied to the earliest occuring touch point
29 | >- U-shaped - 40% conversion credit is given to both first and last touches, with the remaining 20% split across all others
30 |
31 | 🧠 For more information, [Segment has written an article](https://segment.com/academy/advanced-analytics/an-introduction-to-multi-touch-attribution/) introducing the topic and the most common models.
32 |
33 | ## Configuring the Engine ⚙️
34 |
35 | Instructions on how to configure the MTA Engine can be found [here](docs/configuration.md).
36 |
37 | ## Engine Outputs 🔥
38 |
39 | The engine has two primary output models, attributed touches and attributed conversions.
40 | - [**`attributed_touches`**](models/tasman_mta__attributed_touches.sql) contains all filtered touches (based on the touch rules) across all attribution models that have been attributed to a conversion. Where touches have been attributed, there will be a `conversion_event_id` for that `touch_event_id`, as well as a conversion share value if appropriate.
41 | - [**`attributed_conversions`**](models/tasman_mta__attributed_conversions.sql) is this inverse of the attributed touches and contains all filtered conversions (based on the conversion rules) across all attribution models, whether or not they have attributed to a touch. Each `conversion_event_id` may appear once all multiple times depending on the number of attributed touches. Where `touch_event_id` is null, this indicates that the conversion is unattributed. This is the model that should be used in downstream models to analyse attribution performance.
42 |
43 | ## Performance Tracking 🚀
44 |
45 | Attribution is tricky and it's unlikely that optimal results will be achieved during the initial implementation of this engine - this is because the quality of the outputs are entirely dependent on the quality of the inputs along with tuning of the configurations. As such, a [`performance_history`](models/tasman_mta__performance_history.sql) model has been added that will keep track of each time the attribution engine is run, and collect useful statistics that can help accelerate the implementation as well as monitor key metrics such as attribution rate.
46 |
47 | ## Current Limitations ⚠️
48 |
49 | - Handling conversion shares where the total number of touches is lower than the number of specs. For example, if there are only 2 touches but 3 [attribution rule specs](docs/configuration.md#attribution-rules), then the total conversion shares will not sum to 100%. This needs to be accounted for when analysing the outputs.
50 |
51 | ## Supported Data Warehouses
52 | This package currently supports Snowflake and BigQuery targets.
53 |
54 | ## Contact
55 | This package has been written and is maintained by [Tasman Analytics](https://tasman.ai).
56 |
57 | If you find a bug, or for any questions please open an issue on GitHub.
58 |
59 | [tasman_website_dark_mode]: https://tasman.ai?utm_source=github&utm_medium=internal-referral&utm_campaign=tasman-dbt-mta#gh-dark-mode-only
60 | [tasman_website_light_mode]: https://tasman.ai?utm_source=github&utm_medium=internal-referral&utm_campaign=tasman-dbt-mta#gh-light-mode-only
61 | [tasman_contact]: https://tasman.ai/contact?utm_source=github&utm_medium=internal-referral&utm_campaign=tasman-dbt-mta
62 | [tasman_wordmark_cream]: https://raw.githubusercontent.com/TasmanAnalytics/.github/master/images/tasman_wordmark_cream_500.png#gh-dark-mode-only
63 | [tasman_wordmark_black]: https://raw.githubusercontent.com/TasmanAnalytics/.github/master/images/tasman_wordmark_black_500.png#gh-light-mode-only
--------------------------------------------------------------------------------