Skip to content

Instantly share code, notes, and snippets.

@ceaksan
Last active April 30, 2023 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceaksan/a6ead7d54b3dea91dba1464694d50b82 to your computer and use it in GitHub Desktop.
Save ceaksan/a6ead7d54b3dea91dba1464694d50b82 to your computer and use it in GitHub Desktop.
Shopify - GA4 Content Group Info
{% assign t = template | split: '.' | first %}
{%- assign contentGroupInfo = '' -%}
{%- case t -%}
{%- when 'index' -%}
{%- capture contentGroupInfo -%}{{- contentGroupInfo | append: 'Home' -}}{%- endcapture -%}
{%- when 'page' -%}
{%- capture contentGroupInfo -%}{{- contentGroupInfo | append: 'Pages' -}}{{- contentGroupInfo | append: page.title | prepend: "/" -}}{%- endcapture -%}
{%- when 'cart' -%}
{%- capture contentGroupInfo -%}{{- contentGroupInfo | append: 'Pages/Cart' -}}{%- endcapture -%}
{%- when '404' -%}
{%- capture contentGroupInfo -%}{{- contentGroupInfo | append: 'Pages/404' -}}{%- endcapture -%}
{%- when 'search' -%}
{%- capture contentGroupInfo -%}{{- contentGroupInfo | append: 'Pages/Search' -}}
{%- if search.performed -%}{{- contentGroupInfo | append: search.terms | prepend: "/" -}}{%- endif -%}
{%- endcapture -%}
{%- when 'product' -%}
{%- capture contentGroupInfo -%}
{{- contentGroupInfo | append: 'Collections' -}}
{%- if collection.url -%}{{- contentGroupInfo | append: collection.title | prepend: "/" -}}
{%- else -%}{{- contentGroupInfo | append: product.collections.first.title | prepend: "/" -}}
{%- endif -%}
{{- contentGroupInfo | append: product.title | prepend: "/" -}}
{%- if product.selected_variant -%}{{- contentGroupInfo | append: product.selected_variant.title | prepend: "/" -}}{%- endif -%}
{%- endcapture -%}
{%- when 'list-collections' -%}
{%- capture contentGroupInfo -%}{{- contentGroupInfo | append: 'Collections' -}}{%- endcapture -%}
{%- when 'collection' and collection.handle -%}
{%- capture contentGroupInfo -%}
{{- contentGroupInfo | append: 'Collections' -}}
{%- if current_tags -%}{{- contentGroupInfo | append: collection.title | prepend: "/" -}}{{- contentGroupInfo | append: "Tags" | prepend: "/" -}}{{ current_tags | join: " + " | prepend: "/" }}
{%- else -%}{{- contentGroupInfo | append: collection.title | prepend: "/" -}}
{%- endif -%}
{%- endcapture -%}
{%- when 'blog' -%}
{%- capture contentGroupInfo -%}
{{- contentGroupInfo | append: 'Blog' -}}
{%- if current_tags -%}{{- contentGroupInfo | append: blog.title | prepend: "/" -}}{{- contentGroupInfo | append: "Tags" | prepend: "/" -}}{{ current_tags | join: " + " | prepend: "/" }}
{%- else %}{{- contentGroupInfo | append: blog.title | prepend: "/" -}}
{%- endif -%}
{%- endcapture -%}
{%- when 'article' -%}
{%- capture contentGroupInfo -%}
{{- contentGroupInfo | append: 'Blog' -}}
{{- contentGroupInfo | append: blog.title | prepend: "/" -}}
{{- contentGroupInfo | append: article.title | prepend: "/" -}}
{%- endcapture %}
{%- else -%}
{%- capture contentGroupInfo -%}
{{- contentGroupInfo | append: 'Home' -}}
{{- contentGroupInfo | append: page_title | prepend: "/" -}}
{%- endcapture -%}
{%- endcase -%}
<script>
dataLayer.push({
'content_group': "{{ contentGroupInfo | remove: "'" | remove: '"' }}"
});
</script>
{% comment %}
{% unless template == 'index' or template == 'cart' or template == 'list-collections' or template == '404' %}{% endunless %}
{% endcomment %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment