-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_visitor.py
More file actions
132 lines (106 loc) · 4.33 KB
/
update_visitor.py
File metadata and controls
132 lines (106 loc) · 4.33 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
"""
Update Visitor Example
This example demonstrates how to update an existing visitor using their UUID.
Use this when you have the visitor's UUID from your database or a previous API call.
Difference from identify:
- identify: Uses LBID (from tracking) - finds OR creates visitor
- update: Uses UUID (from database) - only updates existing visitor
Use Cases:
- User updates their profile information
- User upgrades/downgrades their subscription
- Scheduled job enriches visitor data from external sources
- Real-time updates based on user actions in your app
"""
import os
from datetime import datetime
from linkbreakers import (
Configuration,
ApiClient,
VisitorsApi,
VisitorsServiceUpdateBody,
VisitorInput
)
def update_visitor():
"""Update an existing visitor by UUID"""
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)
# Visitor UUID
#
# This comes from:
# 1. Your database (stored when you first identified the visitor)
# 2. Previous API response (from identify or get visitor)
# 3. Webhook payload
#
# Format: Standard UUID (e.g., "550e8400-e29b-41d4-a716-446655440000")
visitor_id = '550e8400-e29b-41d4-a716-446655440000'
try:
visitor = visitors_api.visitors_service_update(
id=visitor_id,
visitors_service_update_body=VisitorsServiceUpdateBody(
visitor=VisitorInput(
data={
# Update system fields
'$email': 'john.doe.updated@example.com',
'$phone': '+14155559999',
# Update custom attributes
'plan': 'enterprise',
'planUpgradedAt': datetime.now().isoformat(),
'mrr': 499,
'seats': 10,
'lastActivity': datetime.now().isoformat(),
# Add new custom attributes
'customDomain': 'acme.example.com',
'ssoEnabled': True
}
)
)
)
print('✓ Visitor updated successfully')
print(f' - Visitor ID: {visitor.id}')
print(f' - Email: {visitor.email}')
print(f' - Updated attributes: {visitor.attributes}')
return visitor
except Exception as error:
print(f'✗ Failed to update visitor: {error}')
raise
def batch_update_visitors(visitor_updates):
"""
Batch update multiple visitors
Useful for scheduled jobs or bulk operations
"""
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)
print(f'Updating {len(visitor_updates)} visitors...')
results = []
for update in visitor_updates:
try:
visitor = visitors_api.visitors_service_update(
id=update['id'],
visitors_service_update_body=VisitorsServiceUpdateBody(
visitor=VisitorInput(data=update['data'])
)
)
print(f' ✓ Updated visitor {update["id"]}')
results.append({'status': 'success', 'visitor': visitor})
except Exception as error:
print(f' ✗ Failed to update visitor {update["id"]}: {error}')
results.append({'status': 'error', 'error': str(error)})
succeeded = len([r for r in results if r['status'] == 'success'])
failed = len([r for r in results if r['status'] == 'error'])
print(f'\nBatch update complete: {succeeded} succeeded, {failed} failed')
return results
if __name__ == '__main__':
try:
update_visitor()
print('\n✓ Example completed successfully')
except Exception as error:
print(f'\n✗ Example failed: {error}')
exit(1)