-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from botstory/feature/google-analytics-interface
Feature/google analytics interface
- Loading branch information
Showing
26 changed files
with
1,110 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
from ..integrations.mocktracker import tracker | ||
|
||
logger = logging.getLogger(__name__) | ||
_tracker = None | ||
|
||
|
||
def add_tracker(tracker): | ||
logger.debug('add_tracker') | ||
logger.debug(tracker) | ||
global _tracker | ||
_tracker = tracker | ||
|
||
|
||
def on_new_user_comes(user): | ||
""" | ||
each interface that create new user should call this method. | ||
as well if someone need to catch new user it is good place | ||
to do it | ||
:param user: | ||
:return: | ||
""" | ||
_tracker.new_user(user) | ||
|
||
|
||
def clear(): | ||
global _tracker | ||
_tracker = tracker.MockTracker() | ||
|
||
|
||
clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
interface.tracker | ||
----------------- | ||
|
||
Collects statistics of usage like path through stories. | ||
|
||
Implementations | ||
~~~~~~~~~~~~~~~ | ||
|
||
- `Google Analytics <https://github.com/botstory/botstory/blob/develop/botstory/integrations/ga/tracker.py>`_ | ||
|
||
*Usage:* | ||
.. code-block:: python | ||
from botstory.integrations.ga import tracker | ||
story.use(tracker.GAStatistics(tracking_id='UA-XXXXX-Y')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import functools | ||
import json | ||
from .universal_analytics.tracker import Tracker | ||
|
||
from ...utils import queue | ||
|
||
|
||
class GAStatistics: | ||
type = 'interface.tracker' | ||
""" | ||
pageview: [ page path ] | ||
event: category, action, [ label [, value ] ] | ||
social: network, action [, target ] | ||
timing: category, variable, time [, label ] | ||
""" | ||
|
||
def __init__(self, | ||
tracking_id, | ||
story_tracking_template='{story}/{part}', | ||
new_message_tracking_template='receive: {data}', | ||
): | ||
""" | ||
:param tracking_id: should be like UA-XXXXX-Y | ||
""" | ||
self.tracking_id = tracking_id | ||
self.story_tracking_template = story_tracking_template | ||
self.new_message_tracking_template = new_message_tracking_template | ||
|
||
def get_tracker(self, user): | ||
return Tracker( | ||
account=self.tracking_id, | ||
client_id=user and user['_id'], | ||
) | ||
|
||
def event(self, user, | ||
event_category=None, | ||
event_action=None, | ||
event_label=None, | ||
event_value=None, | ||
): | ||
queue.add( | ||
functools.partial(self.get_tracker(user).send, | ||
'event', event_category, event_action, event_label, event_value | ||
) | ||
) | ||
|
||
def story(self, user, story_name, story_part_name): | ||
queue.add( | ||
functools.partial(self.get_tracker(user).send, | ||
'pageview', self.story_tracking_template.format(story=story_name, | ||
part=story_part_name), | ||
) | ||
) | ||
|
||
def new_message(self, user, data): | ||
queue.add( | ||
functools.partial(self.get_tracker(user).send, | ||
'pageview', self.new_message_tracking_template.format(data=json.dumps(data)), | ||
) | ||
) | ||
|
||
def new_user(self, user): | ||
queue.add( | ||
functools.partial(self.get_tracker(user).send, | ||
'event', | ||
'new_user', 'start', 'new user starts chat' | ||
) | ||
) |
Oops, something went wrong.