-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_visitors.py
More file actions
260 lines (195 loc) · 8.05 KB
/
list_visitors.py
File metadata and controls
260 lines (195 loc) · 8.05 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
List Visitors Example
This example demonstrates how to query and filter visitors.
Use Cases:
- Export visitor data for analysis
- Find visitors by email or attributes
- Build customer segments
- Search for specific visitors
- Paginate through large visitor lists
"""
import os
from linkbreakers import (
Configuration,
ApiClient,
VisitorsApi
)
def list_all_visitors():
"""Example 1: List all visitors with pagination"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
visitors_api = VisitorsApi(api_client)
try:
response = visitors_api.visitors_service_list(
page_size=50, # Max: 200
# page_token='next-page-token', # For pagination
)
print('✓ Retrieved visitors')
print(f' - Total in this page: {len(response.visitors) if response.visitors else 0}')
print(f' - Next page token: {response.next_page_token or "None (last page)"}')
if response.visitors:
for visitor in response.visitors:
email = visitor.email or 'Anonymous'
print(f'\n Visitor: {email}')
print(f' - ID: {visitor.id}')
print(f' - Name: {visitor.first_name} {visitor.last_name}')
print(f' - Phone: {visitor.phone or "N/A"}')
return response
except Exception as error:
print(f'✗ Failed to list visitors: {error}')
raise
def find_visitor_by_email(email):
"""Example 2: Find a visitor by email"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
visitors_api = VisitorsApi(api_client)
try:
response = visitors_api.visitors_service_list(
email=email, # Exact match filter
page_size=1
)
if response.visitors and len(response.visitors) > 0:
visitor = response.visitors[0]
print('✓ Found visitor')
print(f' - ID: {visitor.id}')
print(f' - Email: {visitor.email}')
print(f' - Attributes: {visitor.attributes}')
return visitor
else:
print(f'✗ No visitor found with email: {email}')
return None
except Exception as error:
print(f'✗ Failed to find visitor: {error}')
raise
def search_visitors(query):
"""Example 3: Search visitors across fields"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
visitors_api = VisitorsApi(api_client)
try:
response = visitors_api.visitors_service_list(
search=query, # Fuzzy search across name, email, attributes
page_size=50
)
visitor_count = len(response.visitors) if response.visitors else 0
print(f'✓ Search results for "{query}"')
print(f' - Found {visitor_count} visitors')
if response.visitors:
for visitor in response.visitors:
email = visitor.email or 'Anonymous'
print(f'\n {email}')
print(f' Name: {visitor.first_name} {visitor.last_name}')
company = visitor.attributes.get('company') if visitor.attributes else None
print(f' Company: {company or "N/A"}')
return response.visitors
except Exception as error:
print(f'✗ Search failed: {error}')
raise
def get_visitors_by_link(link_id):
"""Example 4: Get visitors who clicked a specific link"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
visitors_api = VisitorsApi(api_client)
try:
response = visitors_api.visitors_service_list(
link_id=link_id, # Filter by link UUID
page_size=100,
include=['events'] # Include event data
)
visitor_count = len(response.visitors) if response.visitors else 0
print(f'✓ Visitors who clicked link {link_id}')
print(f' - Total visitors: {visitor_count}')
if response.visitors:
for visitor in response.visitors:
email = visitor.email or 'Anonymous'
event_count = len(visitor.events) if visitor.events else 0
print(f'\n {email}')
print(f' Events: {event_count}')
return response.visitors
except Exception as error:
print(f'✗ Failed to get visitors by link: {error}')
raise
def get_all_visitors_paginated():
"""Example 5: Paginate through all visitors"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
visitors_api = VisitorsApi(api_client)
all_visitors = []
page_token = None
page_num = 1
try:
while True:
print(f'Fetching page {page_num}...')
response = visitors_api.visitors_service_list(
page_size=200, # Max page size
page_token=page_token
)
if response.visitors:
all_visitors.extend(response.visitors)
print(f' ✓ Retrieved {len(response.visitors)} visitors')
page_token = response.next_page_token
page_num += 1
# Break if no more pages
if not page_token:
break
# Optional: Add delay to avoid rate limits
import time
time.sleep(0.1)
print(f'\n✓ Retrieved all {len(all_visitors)} visitors')
return all_visitors
except Exception as error:
print(f'✗ Failed to paginate visitors: {error}')
raise
def export_visitors_to_csv():
"""Example 6: Export visitors to CSV"""
configuration = Configuration(
access_token=os.getenv('LINKBREAKERS_API_KEY', 'your-api-key-here'),
host='https://api.linkbreakers.com'
)
with ApiClient(configuration) as api_client:
visitors_api = VisitorsApi(api_client)
try:
response = visitors_api.visitors_service_list(
page_size=200,
response_format='RESPONSE_FORMAT_CSV'
)
print('✓ Exported visitors to CSV')
print(f' - CSV data length: {len(response.csv) if response.csv else 0}')
# Save to file
from datetime import date
filename = f'visitors-export-{date.today()}.csv'
with open(filename, 'w') as f:
f.write(response.csv)
print(f' - Saved to: {filename}')
return response.csv
except Exception as error:
print(f'✗ Failed to export visitors: {error}')
raise
if __name__ == '__main__':
try:
print('=== List Visitors Examples ===\n')
print('1. List All Visitors:')
list_all_visitors()
print('\n2. Find Visitor by Email:')
find_visitor_by_email('john.doe@example.com')
print('\n3. Search Visitors:')
search_visitors('Acme')
print('\n✓ All examples completed successfully')
except Exception as error:
print(f'\n✗ Examples failed: {error}')
exit(1)