From dadfcb8cb41ccfea80dd1e6dd6b6574eac477ec4 Mon Sep 17 00:00:00 2001 From: aliAgmr Date: Mon, 11 Aug 2025 17:12:18 -0400 Subject: [PATCH 1/6] Refactor Google Analytics integration in head.liquid to use script manager for improved loading and unloading, and added error handling for script loading failures. --- custom_scripts/head.liquid | 177 +++++++++++++++++++++++++++---------- 1 file changed, 129 insertions(+), 48 deletions(-) diff --git a/custom_scripts/head.liquid b/custom_scripts/head.liquid index b366a47..6e8c358 100644 --- a/custom_scripts/head.liquid +++ b/custom_scripts/head.liquid @@ -1,58 +1,139 @@ {%- if settings.measurementId and settings.measurementId.size > 0 -%} {%- assign measurementChar = settings.measurementId | slice: 0 -%} - {%- assign isNew = measurementChar == "G" -%} + {%- assign isNew = measurementChar = = "G" -%} {%- if isNew -%} - - {%- endif -%} {%- unless isNew -%} + {%- endif -%} + {%- unless isNew -%} {%- endunless -%} {%- endif -%} \ No newline at end of file From 693004eb4ab42870e721d437ddf1afbdaed40ff7 Mon Sep 17 00:00:00 2001 From: aliAgmr Date: Mon, 11 Aug 2025 17:12:31 -0400 Subject: [PATCH 2/6] Update CircleCI configuration to include 'dev' branch in workflows for deployment --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1723b24..61a09aa 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -72,6 +72,7 @@ workflows: branches: only: - master + - dev - publish-dev: context: App requires: @@ -80,6 +81,7 @@ workflows: branches: only: - master + - dev - hold: type: approval requires: From 551a8d0ed6eef93973128c4826e9e9ceaf0b95fe Mon Sep 17 00:00:00 2001 From: aliAgmr Date: Mon, 11 Aug 2025 17:20:10 -0400 Subject: [PATCH 3/6] Refactor CircleCI configuration to enhance Docker image build and deployment steps, introduce parameters for registry, and update workflow filters for production deployment. --- .circleci/config.yml | 60 +++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 61a09aa..2e5c55a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,72 +1,60 @@ -# Use the latest 2.1 version of CircleCI pipeline process engine. -# See: https://circleci.com/docs/2.0/configuration-reference version: 2.1 -# Define a job to be invoked later in a workflow. -# See: https://circleci.com/docs/2.0/configuration-reference/#jobs jobs: build: working_directory: /app - # See: https://circleci.com/docs/2.0/configuration-reference/#docker-machine-macos-windows-executor docker: - image: docker:17.05.0-ce-git - environment: - DOCKER_REGISTERY: tribeplatform/google-analytics - # Add steps to the job - # See: https://circleci.com/docs/2.0/configuration-reference/#steps + parameters: + registery: + type: string steps: - checkout - - setup_remote_docker: - version: 19.03.13 + - setup_remote_docker - run: - name: 'Build application Docker image' - command: docker build --cache-from=app -t app . - - deploy: - name: Push application Docker image + name: 'Build and deploy docker image' command: | + docker build --cache-from=app -t app . docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD - docker tag app "${DOCKER_REGISTERY}:${CIRCLE_SHA1}" - docker push "${DOCKER_REGISTERY}:${CIRCLE_SHA1}" + docker tag app << parameters.registery >>:${CIRCLE_SHA1} + docker push << parameters.registery >>:${CIRCLE_SHA1} publish-dev: docker: - - image: tribeplatform/tribecd:0.1.15 + - image: tribeplatform/cli:1.0.0 auth: username: $DOCKER_HUB_USERNAME password: $DOCKER_HUB_PASSWORD environment: DOCKER_REGISTERY: tribeplatform/google-analytics - REVIEW_APP_NAME: google-analytics - REVIEW_APP_ENV: development - REVIEW_APP_REGION: us-east-1 - REVIEW_APP_IMAGE_URI: $DOCKER_REGISTERY:$CIRCLE_SHA1 steps: - checkout - run: - name: 'Publish Image' - command: tcdi -cu + name: 'Deploy app on DEV environment' + command: | + ops init -i -e ermia@bettermode.com -u $OPS_USERNAME -p $OPS_PASSWORD + ops app create -n google-analytics -i $DOCKER_REGISTERY:$CIRCLE_SHA1 + ops service deploy -s google-analytics -i $DOCKER_REGISTERY:$CIRCLE_SHA1 publish-prod: docker: - - image: tribeplatform/tribecd:0.1.15 + - image: tribeplatform/cli:1.0.0 auth: username: $DOCKER_HUB_USERNAME password: $DOCKER_HUB_PASSWORD environment: DOCKER_REGISTERY: tribeplatform/google-analytics - REVIEW_APP_NAME: google-analytics - REVIEW_APP_ENV: production - REVIEW_APP_REGION: us-east-1 - REVIEW_APP_IMAGE_URI: $DOCKER_REGISTERY:$CIRCLE_SHA1 steps: - checkout - run: - name: 'Publish Image' - command: tcdi -cu -# Invoke jobs via workflows -# See: https://circleci.com/docs/2.0/configuration-reference/#workflows + name: 'Deploy app on PRD environment' + command: | + ops init -i -e ermia@bettermode.com -u $OPS_USERNAME -p $OPS_PASSWORD + ops app create -n google-analytics -i $DOCKER_REGISTERY:$CIRCLE_SHA1 -e prd + ops service deploy -s google-analytics -e prd -i $DOCKER_REGISTERY:$CIRCLE_SHA1 workflows: - build_app: + app: jobs: - build: + registery: tribeplatform/google-analytics context: App filters: branches: @@ -86,6 +74,10 @@ workflows: type: approval requires: - build + filters: + branches: + only: + - master - publish-prod: context: App requires: From ffcc04a14f1fd5ca418f917de3a10f361b2db9b9 Mon Sep 17 00:00:00 2001 From: aliAgmr Date: Tue, 12 Aug 2025 16:31:20 -0400 Subject: [PATCH 4/6] Refactor Google Analytics integration in head.liquid to streamline script loading and unloading processes, enhance page view tracking, and improve error handling for script loading failures. --- custom_scripts/head.liquid | 73 ++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/custom_scripts/head.liquid b/custom_scripts/head.liquid index 6e8c358..c497fe5 100644 --- a/custom_scripts/head.liquid +++ b/custom_scripts/head.liquid @@ -8,43 +8,49 @@ name: 'google-analytics', consentCategory: 'analytics', load: () => { + // Load script const script = document.createElement('script'); script.async = true; - script.src = 'https://www.googletagmanager.com/gtag/js?id={{ settings.measurementId }}'; - script.id = 'ga-script'; + script.src = 'https://www.googletagmanager.com/gtag/js?id=G-123456'; document.head.appendChild(script); - // Wait for script to load before initializing - script.onload = () => { - window.dataLayer = window.dataLayer || []; - const gtag = (...args) => dataLayer.push(args); - - gtag('js', new Date()); - gtag('config', '{{- settings.measurementId -}}', { {% if anonymize %}'anonymize_ip': true,{% endif %} 'send_page_view': false }); - gtag('set', {'user_id': '{{ member.id }}'}); - - const onPageView = (data) => gtag('event', 'page_view', { - page_title: data.title, - page_path: data.page, + // Initialize Google Analytics + window.dataLayer = window.dataLayer || []; + function gtag() { + dataLayer.push(arguments); + } + gtag('js', new Date()); + gtag('config', '{{- settings.measurementId -}}', { {% if anonymize %}'anonymize_ip': true,{% endif %} 'send_page_view': false }); + gtag('set', { + 'user_id': '{{- member.id -}}' + }); + + + // Listen for page views + function onPageView(data) { + gtag('event', 'page_view', { + page_title: data.title, + page_path: data.page, }); - - if (window.Tribe) { + } + if (window.Tribe) { window.ga4PageViewListener = window.Tribe.on('pageView', onPageView); - } - }; - - script.onerror = () => { - console.error('Failed to load Google Analytics script'); - }; + } }, unload: () => { - const script = document.getElementById('ga-script'); + // Remove script + const script = document.querySelector('script[src*="googletagmanager.com/gtag/js"]'); if (script) { script.remove(); } - + + // Remove dataLayer + window.dataLayer = undefined; + + // Remove page view listener if (window.ga4PageViewListener) { - window.ga4PageViewListener.remove(); + window.ga4PageViewListener.removeAllListeners(); + window.ga4PageViewListener = undefined; } }, getCookiesToRemove: () => { @@ -74,6 +80,7 @@ name: 'google-analytics-legacy', consentCategory: 'analytics', load: () => { + // Load script (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { @@ -86,14 +93,15 @@ m.parentNode.insertBefore(a, m) })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); - + // Initialize Google Analytics ga('create', '{{ settings.measurementId }}', 'auto'); {% if anonymize -%} ga('set', 'anonymizeIp', true); {% endif -%} ga('set', 'userId', '{{ member.id }}'); - + + // Listen for page views const onPageView = (data) => { ga('send', { 'hitType': 'pageview', @@ -101,19 +109,24 @@ 'title': data.title, }); } - if (window.Tribe) { window.ga4PageViewListener = window.Tribe.on('pageView', onPageView); } }, unload: () => { + // Remove script const script = document.querySelector('script[src*="google-analytics.com/analytics.js"]'); if (script) { script.remove(); } - + + // Remove dataLayer + window.dataLayer = undefined; + + // Remove page view listener if (window.ga4PageViewListener) { - window.ga4PageViewListener.remove(); + window.ga4PageViewListener.removeAllListeners(); + window.ga4PageViewListener = undefined; } }, getCookiesToRemove: () => { From 9d44c4d57606a30370aa1e809dbf43b90c24446f Mon Sep 17 00:00:00 2001 From: aliAgmr Date: Wed, 13 Aug 2025 09:57:29 -0400 Subject: [PATCH 5/6] Enhance Google Analytics integration in head.liquid by adding conditional checks for CMP providers, improving script loading logic, and ensuring proper page view tracking based on measurement ID. --- custom_scripts/head.liquid | 88 ++++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 12 deletions(-) diff --git a/custom_scripts/head.liquid b/custom_scripts/head.liquid index c497fe5..af0b72a 100644 --- a/custom_scripts/head.liquid +++ b/custom_scripts/head.liquid @@ -1,9 +1,18 @@ +{% assign has_cmp_provider = false %} + +{% for item in network.customCodes %} + {% if item.isCmpProvider %} + {% assign has_cmp_provider = true %} + {% endif %} +{% endfor %} + {%- if settings.measurementId and settings.measurementId.size > 0 -%} {%- assign measurementChar = settings.measurementId | slice: 0 -%} {%- assign isNew = measurementChar = = "G" -%} - {%- if isNew -%} - - {%- endif -%} - {%- unless isNew -%} - + {%- endif -%} + {%- unless isNew -%} + - {%- endunless -%} + }) + + {%- endunless -%} + {% unless has_cmp_provider %} + {%- if isNew -%} + + + {%- endif -%} + {%- unless isNew -%} + + {%- endunless -%} + {% endunless %} + {%- endif -%} {%- endif -%} \ No newline at end of file From c37b61bd53208439c8dcdc0526be841cec47dfaa Mon Sep 17 00:00:00 2001 From: aliAgmr Date: Mon, 18 Aug 2025 10:14:26 -0400 Subject: [PATCH 6/6] Revert "Enhance Google Analytics integration in head.liquid by adding conditional checks for CMP providers, improving script loading logic, and ensuring proper page view tracking based on measurement ID." --- custom_scripts/head.liquid | 88 ++++++-------------------------------- 1 file changed, 12 insertions(+), 76 deletions(-) diff --git a/custom_scripts/head.liquid b/custom_scripts/head.liquid index af0b72a..c497fe5 100644 --- a/custom_scripts/head.liquid +++ b/custom_scripts/head.liquid @@ -1,18 +1,9 @@ -{% assign has_cmp_provider = false %} - -{% for item in network.customCodes %} - {% if item.isCmpProvider %} - {% assign has_cmp_provider = true %} - {% endif %} -{% endfor %} - {%- if settings.measurementId and settings.measurementId.size > 0 -%} {%- assign measurementChar = settings.measurementId | slice: 0 -%} {%- assign isNew = measurementChar = = "G" -%} - {% if has_cmp_provider %} - {%- if isNew -%} - - {%- endif -%} - {%- unless isNew -%} - + {%- endif -%} + {%- unless isNew -%} + - {%- endunless -%} - {% unless has_cmp_provider %} - {%- if isNew -%} - - - {%- endif -%} - {%- unless isNew -%} - - {%- endunless -%} - {% endunless %} - {%- endif -%} + }) + + {%- endunless -%} {%- endif -%} \ No newline at end of file