Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ecomerce tracking to complete order #92

Open
PetrDlouhy opened this issue Dec 15, 2018 · 2 comments
Open

Add ecomerce tracking to complete order #92

PetrDlouhy opened this issue Dec 15, 2018 · 2 comments

Comments

@PetrDlouhy
Copy link
Collaborator

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 %}
@Alir3z4
Copy link
Collaborator

Alir3z4 commented Dec 15, 2018

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 ?

@PetrDlouhy
Copy link
Collaborator Author

PetrDlouhy commented Jan 24, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants