This project implements session-based UTM parameter tracking using Google Tag Manager (GTM). It aims to accurately capture the original traffic source for a user's session and persist this information for subsequent analysis and attribution.
The system utilizes browser cookies to store UTM parameters from the user's initial landing page. Google Tag Manager (GTM) is then used to read these cookie values and pass them to various analytics and marketing platforms. This ensures that even if a user navigates away and returns, or if their conversion happens on a different page without UTM parameters in the URL, the original source is still attributed correctly.
The general flow is:
- User lands on the website with UTM parameters in the URL.
- A script (managed via GTM) captures these UTM parameters.
- The script stores these parameters in browser cookies (e.g., first-touch, last-touch, session-based).
- GTM variables are configured to read values from these cookies.
- GTM tags use these variables to send the UTM data to analytics tools, CRMs, etc.
docs/: Contains detailed documentation on the implementation, including:- Introduction and prerequisites.
- Cookie strategy and GTM variable/trigger/tag configurations.
- Data flow diagrams and testing guides.
- Advanced topics and customization options.
src/: Contains JavaScript code snippets that can be used as Custom HTML tags in GTM. These scripts handle tasks like:- Setting and getting cookie values.
- Parsing UTM parameters from URLs or referrer information.
- Populating hidden form fields with UTM data.
gtm_container_export.json: A GTM container export file that includes pre-configured tags, triggers, and variables for this tracking setup. This can be imported into your GTM account as a starting point.
- Read the Documentation: Start by reading the files in the
docs/directory to understand the concepts, strategy, and setup process. - Use the Code Snippets: The JavaScript files in
src/provide ready-to-use or adaptable code for your GTM Custom HTML tags. - Reference GTM Export: The
gtm_container_export.jsonfile can be imported into your GTM workspace. Review and adapt the imported tags, triggers, and variables to fit your specific website structure and analytics needs.
The gtm_container_export.json file provided in this repository is a template/example only. It does not contain any sensitive or production-specific information. Before importing this file into your GTM account, please note:
- Sanitize Before Use: If you adapt this file for your own use, ensure all sensitive information (such as account IDs, container IDs, or proprietary tags) is removed or replaced.
- For Test/Reference Use: This export is intended as a starting point for a test container. Do not use it directly in production without thorough review and customization.
- Review and Adapt: After importing, review all tags, triggers, and variables to ensure they match your website's requirements and do not conflict with existing GTM configurations.
- Test Thoroughly: Use the
docs/08-testing-guide.mdto ensure the tracking is working as expected.
- Accurate Session Attribution: Captures and persists the initial traffic source for a user's entire session, leading to more reliable marketing attribution.
- GTM-Centric: Leverages the power and flexibility of Google Tag Manager for implementation and management.
- Customizable: The provided code and GTM setup can be customized to fit specific business requirements and tracking scenarios.