Hello,
How do you target contacts based on how many miles away they are from a given location? Targetting by country or city will not work.
HS Support told me this feature is not built into HubSpot, so I’m wondering what the community does to accomplish this.
@kyleffotf - yep. Its perplexing to have this functionality kind of exposed in HubDB, but no elsewhere.
For what its worth, a cleverer person than myself donated this algorithm to deduce distance based on a pair ot Lat/Long coordinates:
def calculate_distance(lat1, lon1, lat2, lon2):
R = 6371.0 # radius of Earth in kilometers. Use 3956 for miles
#lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
lat2r = math.radians(lat1)
lat1r = math.radians(lat2)
lon1r = math.radians(lon1)
lon2r = math.radians(lon2)
dlon = lon2r - lon1r
dlat = lat2r - lat1r
a = math.sin(dlat / 2)**2 + math.cos(lat1r) * math.cos(lat2r) * math.sin(dlon / 2)**2
sqrt_a = math.sqrt(a)
c = 2 * math.atan2(sqrt_a, math.hypot(1, -sqrt_a))
distance = R * c
return math.floor(distance)
I use a 3rd party API from Zipcodedownload.com to get lat/long based on other data.
Hope this helps.
Steve
Thanks Steve.
How exactly do you implement this function? I’m looking for a way to send emails to contacts within a specified range.
Use of Python in a workflow step requires Ops Hub Pro. One way I have set this up is to determine a base location, then assign a property inside each contact that gives a distance from that base location. Then you can set up a dynamic list that gives you a way to target emails based on your range parameter.
Steve