I was looking through the API documentation for Marketing Cloud, here. But it is unclear how to get the count of ALL contact in MC. I know that I can log in and see the number there, but what if I want to retrieve that number programmatically? Preferably using REST API
A seemingly simple question, but you really have to involve a few things in SFMC.
A bit of a chain of things to prepare inside SFMC, but then pretty straightforward. Note, as you will need automation studio involve, you would get this on an hourly update basis, not in real time.
The main missing link is described here.
Note: You must have MobileConnect or MobilePush to complete these steps.
- In MobileConnect or MobilePush, create a filtered mobile list
Let's hope you have that.
If you perform the step from MobilePush or GroupConnect, you can use automation studio to auto-refresh ("Refresh mobile filtered list").
You can then run SQL queries on that list as if it were a Data Extension as seen in the link.
With the resulting data extension, you can work in any way that is supported by DEs. Retrieves through the API are capped at 2000 or 2500 records, I forget. So you need to count the rows internally first, and retrieve the resulting number instead of "counting by API".
However, there seems to be an undocumented REST endpoint for just that:
Of course the general advice is to not use this in production, so here's how to go the long way without it:
use an SQL COUNT. or...
/End edit /
count with AMPScript, function DataExtensionRowCount().
https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-programmatic-content.meta/mc-programmatic-content/dataextensionrowcount.htm of the function into another DE with ampscript.
Put that AMPScript function counting your relevant DE into a content block of type code snippet, and call it from the automation using a script activity.
So "in short":
Have Mobile Connect
Create Filtered List; do this from either MobilePush or GroupConnect so the next step can be done.
Refresh Filter automatically in Automation Studio ("Refresh mobile filtered list")
Run SQL Query to get the list data as Data Extension
a) end of automation; Count using API using undocumented endpoint
- b) SQL COUNT and retrieve resulting DE; then end of automation
- c.1 create code snippet with ampscript that counts and writes the number into a DE
- List item
- c.2 in automation: call code snippet via Script activity
- c.3 through API, retrieve field from resulting "count Data extension"