我正在使用 pyga python 模块来做一些谷歌分析报告服务器端(而不是 ga.js)。
PYGA: https ://pypi.python.org/pypi/pyga
到目前为止一切都很好..但我目前在将“项目”添加到 track_transaction() 时遇到问题。它似乎需要某种类型的数组或列表,我在 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')