陷入了蟒蛇问题

时间:2013-10-27 23:57:18

标签: python google-analytics

我正在使用pyga python模块来执行一些Google Analytics报告服务器端(而不是ga.js)。

PYGA: https://pypi.python.org/pypi/pyga

到目前为止一切都很好..但我现在在向track_transaction()添加“items”时遇到问题。它似乎想要某种类型的数组或列表,我在python中不够流利来分解它。任何帮助都将非常感激

我试过这些无济于事:

trans.items = {"order_id":"12222","sku":"PP1","name":"Donation","price":"0.44", "quanity":"1"}

trans.items=[ ['order_id', '12222'], ['sku', 'PayPal1'], ['name', 'Donation'], ['price', '0.44'], ['quanity', '1'] ]

trans.items = [12222,'PP1','Donation','0.44',1,1]

gaitems = Item()
gaitems.order_id='12222'
gaitems.sku='PP1'
gaitems.name='Test'
gaitems.price='0.44'
gaitems.quanity='1'
gaitems.variation='1'
trans.items=gaitems


tracker.track_transaction(trans, session, visitor)

这就是我用来实例化的内容


tracker = Tracker('UA-XXXXXXX-X', 'domain.com')
        visitor = Visitor()
        visitor.ip_address = '192.168.1.1'
        session = Session()
        page = Page('/index.html')
        tracker.track_pageview(page, session, visitor)

        # Transaction
        trans = Transaction()
        trans.order_id = '001000'
        trans.affiliation = 'paypal'
        trans.total = '0.44'
        trans.tax = '0.00'
        trans.shipping = '0.00'
        trans.city ='NA'
        trans.state ='NA'
        trans.country = 'US'

        tracker.track_transaction(trans, session, visitor)

跟踪交易功能


def track_transaction(self, transaction, session, visitor):
    '''Combines _addTrans(), _addItem() (indirectly) and _trackTrans() of GA Javascript client.'''
    transaction.validate()

    params = {
        'config': self.config,
        'tracker': self,
        'visitor': visitor,
        'session': session,
        'transaction': transaction,
    }
    request = TransactionRequest(**params)
    request.fire()

    for item in transaction.items:
        item.validate()

        params = {
            'config': self.config,
            'tracker': self,
            'visitor': visitor,
            'session': session,
            'item': item,
        }
        request = ItemRequest(**params)
        request.fire()

交易类


class Transaction(object):
'''
Represents parameters for a Transaction call

Properties:
order_id -- Order ID, will be mapped to "utmtid" parameter
affiliation -- Affiliation, Will be mapped to "utmtst" parameter
total -- Total Cost, will be mapped to "utmtto" parameter
tax -- Tax Cost, will be mapped to "utmttx" parameter
shipping -- Shipping Cost, values as for unit and price, will be mapped to "utmtsp" parameter
city -- Billing City, will be mapped to "utmtci" parameter
state -- Billing Region, will be mapped to "utmtrg" parameter
country -- Billing Country, will be mapped to "utmtco" parameter
items -- @entity.Items in a transaction

'''

def __init__(self):
    self.items = []
    self.order_id = None
    self.affiliation = None
    self.total = None
    self.tax = None
    self.shipping = None
    self.city = None
    self.state = None
    self.country = None

def __setattr__(self, name, value):
    if name == 'order_id':
        for itm in self.items:
            itm.order_id = value
    object.__setattr__(self, name, value)

def validate(self):
    if len(self.items) == 0:
        raise exceptions.ValidationError('Transaction need to consist of at least one item')

def add_item(self, item):
    ''' item of type entities.Item '''
    if isinstance(item, Item):
        item.order_id = self.order_id
        self.items.append(item)

项目类


class Item(object):
'''
Represents an Item in Transaction

Properties:
order_id -- Order ID, will be mapped to "utmtid" parameter
sku -- Product Code. This is the sku code for a given product, will be mapped to "utmipc" parameter
name -- Product Name, will be mapped to "utmipn" parameter
variation -- Variations on an item, will be mapped to "utmiva" parameter
price -- Unit Price. Value is set to numbers only, will be mapped to "utmipr" parameter
quantity -- Unit Quantity, will be mapped to "utmiqt" parameter

'''

def __init__(self):
    self.order_id = None
    self.sku = None
    self.name = None
    self.variation = None
    self.price = None
    self.quantity = 1

def validate(self):
    if not self.sku:
        raise exceptions.ValidationError('sku/product is a required parameter')

1 个答案:

答案 0 :(得分:2)

说实话,您应该使用trans.add_item(my_item) my_item Item的实例,而不是尝试手动将列表分配给trans.items

相关问题