kra3/py-ga-mob

unique_id in Visitor should not be lazy

jaysonsantos opened this issue · 7 comments

Actually unique_id in Visitor is lazy and that will be a problem if you try to serialize it before unique_id is used, for example if

try:
    visitor = loads(user.googleanalytics.serialized_user.encode('ascii'))
except GoogleAnalytics.DoesNotExist:
    visitor = Visitor()
    visitor.ip_address = '8.8.8.8'
    GoogleAnalytics.objects.create(user=user, serialized_user=dumps(visitor))

tracker = Tracker('UA-12-13', 'domain.com')

page = Page(request_data.get('page', ''))

tracker.track_pageview(page, Session(), visitor)

As it is serialized and saved in database before tracker.track_pageview, unique_id was never acessed so it will be None and if you deserialize it and access unique_id, it will not be the same.
I think, it could be like this jaysonsantos@861f149

kra3 commented

I'll have a look. Thank you for reporting.

On 4 July 2012 05:24, Jayson Reis <
reply@reply.github.com

wrote:

Actually unique_id in Visitor is lazy and that will be a problem if you
try to serialize it before unique_id is used, for example if

try:
    visitor = loads(user.googleanalytics.serialized_user.encode('ascii'))
except GoogleAnalytics.DoesNotExist:
    visitor = Visitor()
    visitor.ip_address = '8.8.8.8'
    GoogleAnalytics.objects.create(user=user,
serialized_user=dumps(visitor))

tracker = Tracker('UA-12-13', 'domain.com')

page = Page(request_data.get('page', ''))

tracker.track_pageview(page, Session(), visitor)

As it is serialized and saved in database before tracker.track_pageview,
unique_id was never acessed so it will be None and if you deserialize it
and access unique_id, it will not be the same.
I think, it could be like this
jaysonsantos@861f149


Reply to this email directly or view it on GitHub:
#3

kra3 commented

The idea for making it lazy is that, I'll be able to include as many user specific info to generate that unique key.

see generate_hash() in Visitor

So as late as we can push the generation of unique key, the more "unique" the key will become.

BTW., uniqueid will be accessed when you call Tracker functions like track_pageview. Which is needed to build cookies.
Usually, one doesn't have to set that manually.

Can you tell me what's the error that you are getting with current approach, if any?

What about getstate? If the user serialize object before access, it will at least generate it's unique_id.

Answering your question, the problem is because I serialize it, before tack_pageview is called, so it will be None.
Here is an example

from cPickle import loads, dumps
from pyga.requests import Visitor

visitor = Visitor()

assert loads(dumps(visitor)).unique_id == visitor.unique_id
kra3 commented

I'm Okey with implementing __getstate__() , if you want to provide a patch (after trying it yourself), I can put that in and release an update by tomorrow.

PS: I understood what's happening when you serialize Visitor, but is there any particular logic associated with unique_id of Visitor ?

I will implement it and do a pull request.

About PS: It has, because I serialize it and save it in my db to my user profile, and if unique_id is different, every request in analytics will count as a new visitor.

kra3 commented

Hmm, nice to see another way to use the library. I adds Visitor to user's session.