私は pyga python モジュールを使用して、(ga.js の代わりに) Google Analytics レポート サーバー側を実行しています。
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')