# Custom Code to Count Activities

**URL:** <https://community.hubspot.com/t/custom-code-to-count-activities/80939>\
**Category:** Data Hub\
**Created:** [May 10, 2023, 3:44pm UTC](https://community.hubspot.com/t/custom-code-to-count-activities/80939 "2023-05-10T15:44:00Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![KimM](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kimm/32/98942_2.png) [@KimM](https://community.hubspot.com/u/KimM)\
**Post date:** [May 16, 2023, 6:21pm UTC](https://community.hubspot.com/t/custom-code-to-count-activities/80939/7 "2023-05-16T18:21:20Z")

</div>

I would suggest doing this using the Hubspot engagements calls API utilising the search endpoint: [Accounts Dashboard | HubSpot](https://developers.hubspot.com/docs/api/crm/calls)

You can filter based on specific criteria that you are looking at for each call you want to count. ie, Call Status is completed, or a specific time frame you would like to look at, or specific users you want to track.

You can then extract the ‘total’ value from the api response which is a count of all the calls that match your search.

Here’s a little sample of how I’ve done it in the past:

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({"accessToken":process.env.YOUR_SECRET});
var id = event.inputFields['id'];
const PublicObjectSearchRequest = {
 filterGroups: [
 {
 "filters":[
 {"propertyName":"associations.contact",
 "operator":"EQ",
 "value": id
 }
 // ADD ANY OTHER FILTERS YOU WOULD LIKE TO USE HERE. MAX OF 3 PER FILTER GROUP.
 ]
 }
 ],
 properties:[]
};
var count_of_calls;
try {
 const apiResponse = await hubspotClient.crm.objects.emails.searchApi.doSearch(PublicObjectSearchRequest);
 count_of_calls = apiResponse.total
} catch (e) {
 console.log(e);
 throw e
}
 callback({
 outputFields: {
 count_of_calls: count_of_calls
 }
 });
}

```

You will obviously need to add your own secret key into this code, and add the contact record id as an input field.

Hope this helps!

---

_[View the full topic](https://community.hubspot.com/t/custom-code-to-count-activities/80939)._
