Login

LinkShare Pixel Tracking template tag for Satchmo

Author:
maxk
Posted:
January 7, 2011
Language:
Python
Version:
1.2
Score:
0 (after 0 ratings)

Code for template tag which generate LinkShare Pixel Tracking code for Satchmo-based stores.

Paste code into linkshare_tracking.py file in your project's template tags folder. Set LINKSHARE_MERCHANT_ID in your settings.py

Usage: Put at top of success.html template: {% load linkshare_tracking %}

Somewhere in success.html template body: {% linkshare_pixeltracker request order %}

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
import logging
import urllib

from decimal import Decimal

from django import template
from django.conf import settings

register = template.Library()
log = logging.getLogger('store.linkshare_tracking')

DEFAULT_MERCHANT_ID = "123"
PREPARE_LINKSHARE_VALUES = lambda arr: "|".join([str(vl) for vl, item in arr])


@register.simple_tag
def linkshare_pixeltracker(request, order):
    """Method to generate linkshare pixel tracking code
    Usage:
    {% load linkshare_tracking %}

    
    {% linkshare_pixeltracker request order %}
    
    """
    linkshare_host = 'track.linksynergy.com/ep'
    merchant_id = getattr(settings, \
            'LINKSHARE_MERCHANT_ID', DEFAULT_MERCHANT_ID)

    def get_sku_list(order):
        """Get list of SKU of products in order"""
        products_sku = []
        for item in order.orderitem_set.all():
            products_sku.append((item.product.sku, item))
            if item.discount:
                products_sku.append(('Discount', item))
        return products_sku

    def get_qtt_list(order):
        """Get list of quantities of products in order"""
        products_qtt = []
        for sku, item in get_sku_list(order):
            if sku.lower() == 'discount':
                continue
            products_qtt.append((item.quantity.to_integral(), item))
            if item.discount:
                products_qtt.append(('0', item))
        return products_qtt

    def get_amt_list(order):
        """Get list of amounts of all items in order.
        Note that all prices should be without discounts"""
        products_amt = []
        for sku, item in get_sku_list(order):
            if sku.lower() == 'discount':
                continue

            total = Decimal('100') * \
                    item.product.unit_price * item.quantity

            products_amt.append((total.to_integral(), item))
            if item.discount:
                discount = item.discount * Decimal('100')
                products_amt.append(\
                    ("-%s" % str(discount.to_integral()), item))

        return products_amt

    def get_names_list(order):
        """Get list of names of products in order
        """
        products_nm = []
        for sku, item in get_sku_list(order):
            if sku.lower() == 'discount':
                continue

            products_nm.append((item.product.name, item))
            if item.discount:
                products_nm.append(('Discount', item))

        return products_nm

    ls_args = {
    'mid': merchant_id,
    'ord': order.id,
    'skulist': PREPARE_LINKSHARE_VALUES(get_sku_list(order)),
    'qlist': PREPARE_LINKSHARE_VALUES(get_qtt_list(order)),
    'amtlist': PREPARE_LINKSHARE_VALUES(get_amt_list(order)),
    'cur': order.currency_code,
    'namelist': PREPARE_LINKSHARE_VALUES(get_names_list(order)),
    }

    # get http or secured https prefix
    proto = 'http'
    if request.is_secure():
        proto = 'https'

    return '<img src="%(proto)s://%(host)s?%(qs)s" />' % {
    'proto': proto,
    'host': linkshare_host,
    'qs': urllib.urlencode(ls_args),
    }

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

darek (on January 11, 2011):

Satchmo is evil.

#

Please login first before commenting.