django-getpaid/django-plans

Add ecomerce tracking to complete order

PetrDlouhy opened this issue · 2 comments

I wanted to set up ecomerce tracking for Google Analytics for my plans. All I had to do is override the plans/order_detail.html template with the following code. Do you have interest to include this in this project?

{% extends "plans/order_detail.html" %}
{% load i18n %}

{% block head %}
    {% if object.get_status_display == 'completed' %}
    <script>
    window.dataLayer = window.dataLayer || [];
    dataLayer.push({
       'transactionId': '{{ object.id }}',
       'transactionTotal': {{ object.total }},
       'transactionTax': {{ object.tax_total }},
       'transactionProducts': [{
           'sku': 'P{{ object.plan.id }}',
           'name': '{{ object.name }}',
           'price': {{ object.amount }},
           'quantity': 1
       }]
    });
    </script>
    {% endif %}
    {{ block.super }}
{% endblock %}

This looks good, but do you mean to have this as default in the project?

It would make sense to have this as an option.
For example, if user added a template file like post_order_complete.html then in the plans/order_detail.html it could be checked and if existed then included.

Makes sense ?

To anyone interested in this, I finally ended up with a bit different code, which works for me and I also added FB Pixel code:

{% load i18n %}

{% block analytics_head %}
    {{ block.super }}
    {% if object.get_status_display == 'completed' %}
    <script>
    ga('require', 'ecommerce');
    ga('ecommerce:addTransaction', {
        'id':  '{{ object.id }}',
        'affiliation':  'Plan purchase',
        'revenue': '{{ object.amount }}',
        'tax': '{{ object.tax_total }}'
    });
    ga('ecommerce:addItem', {
        'id':  '{{ object.id }}',
        'name': '{{ object.name }}',
        'sku': 'P{{ object.plan.id }}',
        'price': '{{ object.amount }}',
        'category': 'Subscription plan',
        'tax': '{{ object.tax_total }}',
        'quantity': '1'
    });
    ga('ecommerce:send');
    </script>
    <script>
      fbq('track', 'Purchase', {
        value: {{ object.amount }},
        currency: 'USD',
        content_ids: '{{ object.id }}',
        content_type: 'plan',
      });
    </script>
    {% endif %}
{% endblock %}

Note: The current code can lead to duplicate reports if the user renew the payment confirmation page.

@Alir3z4 Turning this on by default would be definitely bad thing to do. But I don't quite understand your solution. I think, that it would be nice to offer users code that is included in django-plans codebase and therefore better tested across various projects.
I think, that it should be turned on either by settings variable or by presence of Google analytics (Facebook pixel) script.