forked from amplitude/ampli-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
109 lines (91 loc) · 3.87 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import logging
from dotenv import dotenv_values
from django.http import HttpResponse
from .ampli import *
from .plugins import SegmentPlugin, MyEventIDPlugin
envs = dotenv_values("ampli_app/.env")
api_key = envs['AMPLITUDE_API_KEY']
write_key = envs['SEGMENT_WRITE_KEY']
ampli.load(LoadOptions(client=LoadClientOptions(api_key)))
# add plugin
my_plugin = MyEventIDPlugin()
# segment_plugin = SegmentPlugin(write_key)
ampli.client.add(my_plugin)
# ampli.client.add(segment_plugin)
# remove plugin
ampli.client.remove(my_plugin)
# ampli.client.remove(segment_plugin)
# Create your views here.
def set_user_property(request):
try:
user_id = request.GET.get('user_id')
required_number = request.GET.get('required_number')
optional_array = request.GET.get('optional_array', '')
if (user_id is None) or (required_number is None):
return HttpResponse('user_id and required_number are required')
event = Identify(required_number=float(required_number), optional_array=optional_array.split(',') or None)
ampli.identify(user_id, event)
return HttpResponse('Success')
except Exception:
logging.exception('Error parsing user properties input')
return HttpResponse('Error parsing user properties input')
def set_group_property(request):
try:
group_type = request.GET.get('group_type')
group_name = request.GET.get('group_name')
required_boolean = request.GET.get('required_boolean')
if (group_type is None) or (group_name is None) or (required_boolean is None):
return HttpResponse('group_type, group_name and required_boolean is required')
optional_string = request.GET.get('optional_string', None)
event = Group(required_boolean=bool(required_boolean), optional_string=optional_string)
ampli.group_identify(group_type, group_name, event)
return HttpResponse('Success')
except Exception:
logging.exception('Error parsing group properties input')
return HttpResponse('Error parsing group properties input')
def set_user_group(request):
try:
user_id = request.GET.get('user_id')
group_type = request.GET.get('group_type')
group_name = request.GET.get('group_name')
if (group_type is None) or (group_name is None) or (user_id is None):
return HttpResponse('user_id, group_type and group_name are required')
ampli.set_group(user_id, group_type, group_name.split(','))
return HttpResponse('Success')
except Exception:
logging.exception('Error parsing input')
return HttpResponse('Error parsing input')
def track_event_no_properties(request):
try:
user_id = request.GET.get('user_id')
if not user_id:
return HttpResponse('user_id is required')
ampli.track(user_id, EventNoProperties())
return HttpResponse('Success')
except Exception:
logging.exception('Error parsing input')
return HttpResponse('Error parsing input')
def track_event_with_const_types(request):
try:
user_id = request.GET.get('user_id')
if not user_id:
return HttpResponse('user_id is required')
ampli.event_with_const_types(user_id)
return HttpResponse('Success')
except Exception:
logging.exception('Error parsing input')
return HttpResponse('Error parsing input')
def flush_events(request):
ampli.flush()
# TODO: Add a template to render a page, collect imformation and track a EventWithAllProperties
def track_event_with_all_properties(request):
event = EventWithAllProperties(
required_array=["a", "b"],
required_boolean=False,
required_enum=EventWithAllProperties.RequiredEnum.ENUM_1,
required_number=16.4,
required_integer=3,
required_string="str"
)
ampli.track('user_id', event)
return HttpResponse('Success')