Contacts History

Is there a way to retrive contacts history to run further analysis outside of Hubspot (python pandas)?

Hello @StephaneBadel

HubSpot provides an API that allows you to retrieve data from the platform. To retrieve contacts history, you can use the Contacts API or the Timeline API.

Here are the general steps to retrieve contacts history data using the Contacts API:

Obtain an API key from HubSpot.
Use the Contacts API to fetch a list of contacts and their associated IDs.
Use the Timeline API to fetch the timeline events associated with each contact ID.
You can filter the events to only include contact property changes or other events that are relevant to your analysis.
Store the data in a format that can be loaded into Pandas, such as a CSV file.
Use Pandas to perform the desired analysis on the data.
You can use the requests library in Python to make API requests to HubSpot, and the pandas library to perform data analysis.

Here is an example of how to retrieve contacts history data using the HubSpot API and Python:

import requests
import pandas as pd

# Set up API key and endpoint URLs
api_key = 'your-api-key'
contacts_url = 'https://api.hubapi.com/contacts/v1/lists/all/contacts/all'
timeline_url = 'https://api.hubapi.com/contacts/v1/contact/vid/{}/timeline'

# Fetch list of contacts
params = {'hapikey': api_key}
response = requests.get(contacts_url, params=params)
contacts = response.json()['contacts']

# Fetch timeline events for each contact
timeline_events = []
for contact in contacts:
 vid = contact['vid']
 response = requests.get(timeline_url.format(vid), params=params)
 events = response.json()['results']
 timeline_events.extend(events)

# Convert data to Pandas DataFrame
df = pd.json_normalize(timeline_events)

This code fetches a list of all contacts from HubSpot, then fetches the timeline events associated with each contact using the Timeline API. The resulting data is stored in a Pandas DataFrame for further analysis.

Thanks for your answer ! The timeline GET call response is a 404, im not sure where to actually get the history then..

Thanks for your answer!

However, the timeline URL seems like it gives a 404 response!

Yes, it is possible to retrieve contact history from HubSpot and run further analysis using Python and pandas.

To do this, you can use the HubSpot API to access the contact information and export it as a CSV file. Then, you can use pandas to read in the CSV file and perform any further analysis or manipulations.

Here are the general steps you can follow:

  1. Authenticate with the HubSpot API using an API key or OAuth 2.0.
  2. Use the API to retrieve the contact information, including any relevant properties or engagement data. You can use the Contacts API idea or the Engagements API, depending on the specific data you need.
  3. Export the data as a CSV file using Python’s built-in CSV module or a third-party library like Pandas.
  4. Use pandas to read in the CSV file and perform any desired analysis or manipulations.

Here’s an example code snippet that retrieves contact properties and engagement data for all contacts and exports it as a CSV file:

import requests
import csv

# Set up API credentials and endpoint URL
api_key = ‘YOUR_API_KEY_HERE’
endpoint = ‘https://api.hubapi.com

# Set up API request headers
headers = {‘Authorization’: f’Bearer {api_key}'}

# Define the properties and engagements to retrieve
properties = [‘firstname’, ‘lastname’, ‘email’]
engagements = [‘type’, ‘timestamp’]

# Make the API request to retrieve the contacts
response = requests.get(f’{endpoint}/contacts/v1/lists/all/contacts/all’,
headers=headers,
params={‘property’: properties, ‘engagement’: engagements})

# Extract the contact and engagement data from the response
data = response.json()[‘contacts’]
for contact in data:
contact[‘engagements’] = [engagement[‘engagement’] for engagement in contact[‘engagements’]]

# Export the data as a CSV file
with open(‘contact_data.csv’, ‘w’, newline=‘’) as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=properties + [‘engagements’])
writer.writeheader()
writer.writerows(data)

Hi @StephaneBadel

Yes, you can retrieve contact history data from HubSpot using the HubSpot API and then analyze it using Python and Pandas.
To retrieve contact history data, you can use the `GET /contacts/v1/lists/all/contacts/all` endpoint of the HubSpot Contacts API. This endpoint returns a paginated list of contacts along with their associated timeline events, such as form submissions, email opens, and page views.
Here is an example Python script using the `requests` and `pandas` libraries to retrieve contact history data and analyze it:

import requests
import pandas as pd

# Set up the request headers and query parameters
headers = {‘Authorization’: ‘Bearer YOUR_HUBSPOT_API_KEY’}
params = {‘property’: [‘email’, ‘firstname’, ‘lastname’, ‘hs_object_id’], ‘propertyMode’: ‘value_only’}

# Make the initial request to get the first page of contacts
url = ‘https://api.hubapi.com/contacts/v1/lists/all/contacts/all
response = requests.get(url, headers=headers, params=params)
data = response.json()

# Paginate through the results to get all contacts
while True:
# Extract the contact and event data from the response
contacts = data[‘contacts’]
events = [event for contact in contacts for event in contact[‘timeline’][‘events’]]

# Convert the event data to a Pandas DataFrame
events_df = pd.json_normalize(events)

# Do further analysis on the event data using Pandas
# For example, group the events by type and count the occurrences of each type
event_counts = events_df.groupby(‘eventTypeId’)[‘eventId’].count()

# Make the next request if there are more pages
if ‘has-more’ in data and data[‘has-more’]:
params[‘vidOffset’] = data[‘vid-offset’]
response = requests.get(url, headers=headers, params=params)
data = response.json()
else:
break
In this example, the `Authorization` header should be replaced with your HubSpot API key. You can also adjust the `property` query parameter to include additional contact properties that you want to retrieve.
The code above retrieves all contacts and their associated events, then converts the event data to a Pandas DataFrame and performs some basic analysis on it, such as counting the occurrences of each event type. You can modify the code to perform any other analysis you need on the contact history data.

Please mark it as SOLUTION ACCEPTED if you like the solution.

Thank you!

Thanks a lot for your answer GRajput! One thing, I thought API Key was deprecated and should be replaced by an app token?

Or am I missing something here?

Thanks!

Hello GRajput,

Any feedback? Thanks!

Hello GRajput,

Any help on the 404 response?

Thanks

Discord by default and only option is to search with Google which kicks you to a web browser directly opening to the Google search page for the phrase you’ve highlighted.

If this is something you would like to see, you could suggest it to Discord through their support channels NamesDio, or community forums