Getting the number of leads in a Smart View for each User in a Group individually
Some teams in Close treat the number of leads in particular Smart Views as metrics they'd like to track and report on. Here are some use cases:
- Leads that were created this month and assigned to me
- Leads that I moved into a particular status
- Leads that are assigned to me and "rotting"
In cases where you want to automate the reporting of this data, rather than going in and running the Smart View as each individual user, you can set up a zap that handles all that for you.
What you will need
- A Zapier account that supports multi-step zaps
- A group or groups in Close (Business plan required)
- A Close API key (instructions to get your API key here)
- Your Close Organization ID (we can provide you with this ID if you do not have it)
Trigger - Schedule by Zapier
This is an optional trigger, but the most popular choice and what we'll use in this example. We're going to set up a weekly zap that generates some metrics for users in our Sales Team and Success Team groups.
First, we'll use the "Schedule by Zapier" app and say we want this zap to trigger weekly.
Then, just select a day and time you'd like your zap to run.
Once this is configured, we can move to the next step
Action - Code by Zapier (Run Python)
This next step is going gather all of the users in your Close groups, and run any subsequent step as each user in that group.
To do this, we're going to use the "Code by Zapier" app and select "Run Python" as the action event.
Next, you will want to configure these three input data fields. Your key (the left side input) will need to appear exactly as shown below. Your values (the right side) should be as follows:
- For group names, enter each group name as it appears in Close. If entering multiple, use a comma to seperate
- For API key and Organization ID, enter them directly into the fields as indicated below
Then, you will see a blank Code box. Copy and paste the code below into the Zapier code box.
Copy and paste this into your zap.
import requests import json # Do Not Touch This Part output = [] headers = { 'Content-Type': 'application/json' } auth = (input_data['api_key'], '') # Get all of the memberships so we can have more data than just the user_id org_params = { '_fields': 'memberships' } organization_url = f"https://api.close.com/api/v1/organization/{input_data['organization_id']}" org_request = requests.get(organization_url, auth=auth, params=org_params, headers=headers) if org_request.status_code != 200: return [{ 'success': False, 'reason': 'could not pull organization info' }] memberships = org_request.json()['memberships'] # Get all user_ids that belong to the groups listed above, separated by comma. User ids are stored # in a set, so we deduplicate properly. user_ids = set() group_url = "https://api.close.com/api/v1/group/" group_names = [name.strip().lower() for name in input_data['group_names'].split(',')] groups = requests.get(group_url, auth=auth, params={'_fields': 'id,name'}).json() group_ids = [ group['id'] for group in groups['data'] if group['name'].strip().lower() in group_names] group_params = { '_fields': 'members' } for group_id in group_ids: r = requests.get(f'{group_url}{group_id}/', params=group_params, auth=auth) if r.status_code == 200: members = r.json()['members'] for member in members: user_ids.add(member['user_id']) else: print(f"Failed to pull group members for {group_id}") # Return membership information for people to use from every user in a group. for m in memberships: if m['user_id'] in user_ids: output.append({ 'username': m['user_full_name'], 'user_id': m['user_id'], 'email': m['user_email'], 'phone': m.get('user_phone_formatted') })
Your code box should now look like this.
Once this is done, test the step. Your output should be one of the users in the listed groups, along with additional information about the user, such as email address and phone number.
Understanding the impact on Zapier task volume
It's important to understand the impact of this step on your zapier tasks, and it will essentially multiple your subsequent task steps and the number of users in your groups. For example, if there are 10 users in your groups, and 10 steps in the zap to gather various metrics, this zap will fire 100 tasks each time it runs.
Setting up subsequent steps (Smart View example)
For this example, we're going to show you how to set the zap up to pull the number of leads in a particular Smart View.
Let's say I want to have this report on "number of new leads created last week that were assigned to me."
To do this, we'll use the "Find Number of Leads in Query" action in Close.
When prompted to enter the query, you will want to select Custom, as we will need to enter the search query directly into Zapier for this zap
Once you enter your search query, you'll want to swap out any references to a Username with the output from the code step. This will ensure that each subsequent time the zap runs, it's inserting each username dynamically into the queries to pull the data you need.
![](http://d33v4339jhl8k0.cloudfront.net/docs/assets/5b11ae9b0428632c466a7280/images/60be6e886264f06fc02fea97/file-hQsn3FVVwN.png)
Reporting is most accurate on past data
When setting up these zaps, you'll want to make sure there's not a possibility of new data being added after you run the zap. Because of this, you'll want to set up your queries to reference past dates. For example, choose "last week", "last month", "yesterday" instead of "this week", "this month", or "today".
Once you've set up and tested any subsequent steps, you'll be all set! The most popular final step would be to add a row containing all of the data points to a Google Sheet using the "Create Spreadsheet Row" action.