Created
October 14, 2021 17:54
-
-
Save pwwilson/361ae35b366da81c0061b8fcd5fb2ea8 to your computer and use it in GitHub Desktop.
Backflip Segment -> Hubspot code
This file contains hidden or 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
"""Segment.io analytics library interface.""" | |
from uuid import uuid4 | |
import analytics | |
from django.conf import settings | |
analytics.write_key = settings.SEGMENT_API_KEY | |
def identify_user_persona(user): | |
"""Execute segment identify action for user.""" | |
if settings.DISABLE_SEGMENT: | |
return | |
if user.segment: | |
analytics.identify(str(user.id), { | |
'displayName': f'{user.first_name} {user.last_name}', | |
'firstName': user.first_name, | |
'lastName': user.last_name, | |
'email': user.email | |
}) | |
def identify_registered_user(user): | |
"""Execute segment identify action for user.""" | |
if settings.DISABLE_SEGMENT: | |
return | |
analytics.identify(str(user.id), { | |
"email": user.email, | |
"displayName": f"{user.first_name} {user.last_name}", | |
"firstName": user.first_name, | |
"lastName": user.last_name, | |
"flip": user.flips, | |
"rental": user.rentals, | |
"phone": user.phone_number, | |
"lifecycle_categories": "Member" | |
}) | |
def identify_waitlist_user(model): | |
"""Execute segment identify event for user requests to the waitlist.""" | |
if settings.DISABLE_SEGMENT: | |
return | |
payload = { | |
'displayName': f'{model.first_name} {model.last_name}', | |
'firstName': model.first_name, | |
'lastName': model.last_name, | |
'email': model.email, | |
'became_waitlist_user': True, | |
'lifecycle_categories': 'Marketing Qualified QB' | |
} | |
if model.referred_by_user: | |
payload['referred_by_user'] = f"{model.referred_by_user.first_name} {model.referred_by_user.last_name}" | |
payload['referral_user'] = True | |
analytics.identify(str(uuid4()), payload) | |
def identify_invited_user(model): | |
"""Execute segment identify event for invitation to user.""" | |
if settings.DISABLE_SEGMENT: | |
return | |
analytics.identify(str(model.id), { | |
"email": model.email, | |
"displayName": f"{model.first_name} {model.last_name}", | |
"firstName": model.first_name, | |
"lastName": model.last_name, | |
"became_invited_user": True | |
}) | |
def track_new_loan(model): | |
"""Execute segment track event for new user loan pre-approval.""" | |
if settings.DISABLE_SEGMENT: | |
return | |
# Only legacy events are showing up in Hubspot and properties are not accessible | |
analytics.track(user_id=str(model.user.id), event='Started Loan Application', properties={ | |
"email": model.user.email, | |
"loanId": model.id, | |
"fullAddress": model.property.full_address, | |
"displayName": f"{model.user.first_name} {model.user.last_name}", | |
"purchasePrice": model.purchase_price, | |
"rehabCost": model.rehab_estimate, | |
"afterRepairValue": model.after_repair_value, | |
"closingDate": model.target_close_date, | |
"started_loan_application": True, | |
}, context={ | |
'traits': { | |
'email': model.user.email, | |
'started_loan_application': True, | |
'recent_started_loan_application': model.id | |
} | |
}) | |
# Only legacy events are showing up in Hubspot and properties are not accessible | |
def track_submitted_loan(model): | |
"""Execute segment track event for loan submission.""" | |
if settings.DISABLE_SEGMENT: | |
return | |
# Legacy event in HS | |
analytics.track(user_id=str(model.user.id), event='Submitted Loan Application', properties={ | |
"email": model.user.email, | |
"loanId": model.id, | |
"fullAddress": model.property.full_address, | |
"displayName": f"{model.user.first_name} {model.user.last_name}", | |
"purchasePrice": model.purchase_price, | |
"loanType": model.loan_type, | |
"interestRate": model.interest_rate, | |
"afterRepairValue": model.after_repair_value, | |
"closingDate": model.target_close_date, | |
"submitted_loan_application": True | |
}, context={ | |
'traits': { | |
'email': model.user.email, | |
'submitted_loan_application': True, | |
'recent_submitted_loan_application': model.id | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment