class UserEvent(UserEventBase, BaseMixin, db.Model): """ An event, anything from a page load (typical) to an activity within that page load. """ __tablename__ = 'user_event' event_session_id = db.Column(None, db.ForeignKey('event_session.id'), nullable=False) event_session = db.relationship(EventSession, backref=db.backref( 'events', lazy='dynamic', order_by='UserEvent.created_at')) #: User's IP address ipaddr = db.Column(db.Unicode(45), nullable=True) #: User's browser useragent = db.Column(db.Unicode(250), nullable=True) #: URL url = db.Column(db.Unicode(2038), nullable=True) #: Referrer referrer = db.Column( db.Unicode(2038), nullable=True, default=lambda: request and (unicode_http_header(request.referrer or '')[:2038] or None)) #: HTTP Method method = db.Column(db.Unicode(10), nullable=True) #: Status code status_code = db.Column(db.SmallInteger, nullable=True) #: Event name name = db.Column(db.Unicode(80), nullable=False) #: Custom event data (null = no data saved) data = db.Column(JsonDict, nullable=True)
def new_from_request(cls, request): instance = cls() instance.ipaddr = request and unicode(request.environ['REMOTE_ADDR'][:45]) instance.useragent = request and unicode_http_header(request.user_agent.string)[:250] instance.url = request and request.url[:2038] instance.method = request and unicode(request.method[:10]) instance.name = request and (u'endpoint/' + (request.endpoint or '')[:80]) return instance
def new_from_request(cls, request): instance = cls() instance.created_at = datetime.utcnow() instance.referrer = unicode_http_header(request.referrer)[:2083] if request.referrer else None instance.utm_source = request.args.get('utm_source', u'')[:250] or None instance.utm_medium = request.args.get('utm_medium', u'')[:250] or None instance.utm_term = request.args.get('utm_term', u'')[:250] or None instance.utm_content = request.args.get('utm_content', u'')[:250] or None instance.utm_id = request.args.get('utm_id', u'')[:250] or None instance.utm_campaign = request.args.get('utm_campaign', u'')[:250] or None instance.gclid = request.args.get('gclid', u'')[:250] or None instance.active_at = datetime.utcnow() instance.events = [] return instance
def new_from_request(cls, request): instance = cls() # We need to set a UUID despite the autogenerated default from UuidMixin, because # EventSessionBase is used independently, isn't a db model, and doesn't have a uuid column. instance.uuid = uuid4() instance.created_at = datetime.utcnow() instance.referrer = unicode_http_header(request.referrer)[:2083] if request.referrer else None instance.utm_source = request.args.get('utm_source', u'')[:250] or None instance.utm_medium = request.args.get('utm_medium', u'')[:250] or None instance.utm_term = request.args.get('utm_term', u'')[:250] or None instance.utm_content = request.args.get('utm_content', u'')[:250] or None instance.utm_id = request.args.get('utm_id', u'')[:250] or None instance.utm_campaign = request.args.get('utm_campaign', u'')[:250] or None instance.gclid = request.args.get('gclid', u'')[:250] or None instance.active_at = datetime.utcnow() instance.events = [] return instance