Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexphelps committed Jun 8, 2023
0 parents commit be37c59
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.tmp/
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
client_id: OG1wwMChFvqO6kAsh7iW1K5SvZvceXlgfqT0DpNP
61 changes: 61 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"locations": {
"storefront": {
"global_header": "snippets/global-header.html",
"add_to_cart": "snippets/add-to-cart.html",
"view_product": "snippets/view-product.html",
"start_checkout": "snippets/start-checkout.html",
"complete_checkout": "snippets/complete-checkout.html"
}
},
"settings_schema": [
{
"name": "google_analytics_enabled",
"type": "checkbox",
"label": "Enable Google Analytics",
"help_text": "Enable Google Analytics on your storefront.",
"default": false
},
{
"name": "google_analytics_measurement_id",
"type": "text",
"label": "Google Analytics Measurement ID",
"default": "",
"help_text": "",
"required": false,
"max_length": 250
},
{
"name": "google_adwords_conversion_enabled",
"type": "checkbox",
"label": "Enable Google Adwords Conversion Tracking",
"help_text": "",
"default": false
},
{
"name": "google_adwords_conversion_id",
"type": "text",
"label": "Google Adwords Conversion ID",
"default": "",
"required": false,
"help_text": "Required when Google Adwords Conversion Tracking is enabled.",
"max_length": 250
},
{
"name": "google_adwords_conversion_label",
"type": "text",
"label": "Google Adwords Conversion Label",
"default": "",
"required": false,
"help_text": "Required when Google Adwords Conversion Tracking is enabled.",
"max_length": 250
},
{
"name": "google_analytics_debug_mode",
"type": "checkbox",
"label": "Enable Debug Mode",
"help_text": "Debug mode allows you to monitor transactions in real time.",
"default": false
}
]
}
19 changes: 19 additions & 0 deletions snippets/add-to-cart.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% if app.settings.google_analytics_enabled %}
<script>
gtag('event', 'add_to_cart', {
value: '{{ value|escapejs }}',
currency: '{{ line.price_currency }}',
items: [
{
index: 0,
item_id: '{{ product.id|escapejs }}',
item_name: '{{ product.get_title|escapejs }}',
item_brand: '{{ store_name|escapejs }}',
item_category: '{{ line.product.categories.first|default:'Uncategorised'|escapejs }}',
price: '{{ line.unit_price_incl_tax_incl_discount|escapejs }}',
quantity: '{{ quantity }}'
}
]
});
</script>
{% endif %}
36 changes: 36 additions & 0 deletions snippets/complete-checkout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% if app.settings.google_analytics_enabled %}
<script>
gtag('event', 'purchase', {
transaction_id: '{{ order.number|escapejs }}',
value: {{ order.total_incl_tax }},
tax: {{ order.total_tax }},
shipping: {{ order.shipping_incl_tax }},
currency: '{{ order.currency|escapejs }}',
coupon: {% if order.voucherapplication_set.first.voucher.code %}'{{ order.voucherapplication_set.first.voucher.code }}'{% else %}null{% endif %},
items: [
{% for line in order.lines.all %}
{
index: {{ forloop.counter0 }},
item_id: '{{ line.product.id|escapejs }}',
item_name: '{{ line.title|escapejs }}',
item_brand: '{{ store_name|escapejs }}',
item_category: '{{ line.product.categories.first|default:'Uncategorised'|escapejs }}',
price: '{{ line.unit_price_incl_tax_incl_discount|unlocalize|escapejs }}',
quantity: {{ line.quantity }}
} {% if not forloop.last %}, {% endif %}
{% endfor %}
]
});

{% if app.settings.google_adwords_conversion_enabled %}

gtag('event', 'conversion', {
send_to: '{{ app.settings.google_adwords_conversion_id }}/{{ app.settings.google_adwords_conversion_label }}',
transaction_id: '{{ order.number|escapejs }}',
value: {{ order.total_incl_tax }},
currency: '{{ order.currency|escapejs }}'
});

{% endif %}
</script>
{% endif %}
16 changes: 16 additions & 0 deletions snippets/global-header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% if app.settings.google_analytics_enabled %}

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{ app.settings.google_analytics_measurement_id }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());

gtag('config', '{{ app.settings.google_analytics_measurement_id }}',
{% if user.is_authenticated %} { 'user_id': '{{ user.pk }}' }, {% endif %}
{% if app.settings.google_analytics_debug_mode %} { 'debug_mode': true }, {% endif %}
);
</script>

{% endif %}
22 changes: 22 additions & 0 deletions snippets/start-checkout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% if app.settings.google_analytics_enabled %}
<script>
gtag('event', 'begin_checkout', {
currency: '{{ basket.currency }}',
value: '{{ order_total.incl_tax }}',
coupon: {% if basket.vouchers.first.voucher %}'{{ basket.vouchers.first.code }}'{% else %}null{% endif%},
items: [
{% for line in basket.all_lines %}
{
index: {{ forloop.counter0 }},
item_id: '{{ line.product.id|escapejs }}',
item_name: '{{ line.product.get_title|escapejs }}',
item_brand: '{{ store.name|escapejs }}',
item_category: '{{ line.product.categories.first|default:'Uncategorised'|escapejs }}',
price: '{{ line.unit_price_incl_tax_incl_discount|unlocalize|escapejs }}',
quantity: {{ line.quantity | escapejs }}
}{% if not forloop.last %}, {% endif %}
{% endfor %}
]
});
</script>
{% endif %}
20 changes: 20 additions & 0 deletions snippets/view-product.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% if app.settings.google_analytics_enabled %}
{% purchase_info_for_product request product as session %}
<script>
gtag('event', 'view_item', {
currency: '{{ session.price.currency }}',
value: {{ session.price.excl_tax }},
items: [
{
index: 0,
item_id: '{{ product.id|escapejs }}',
item_name: '{{ product.get_title|escapejs }}',
item_brand: '{{ store_name|escapejs }}',
item_category: '{{ line.product.categories.first|default:'Uncategorised'|escapejs }}',
price: '{{ session.price.excl_tax }}',
quantity: 1
}
]
});
</script>
{% endif %}

0 comments on commit be37c59

Please sign in to comment.