Here is a question that we are repeatedly asked by our developer community:
“How do I get a list of past activities for an individual lead?”
Up until recently, there was no straightforward way to accomplish this using the REST API. But now there is!
The Winter 2016 release of our REST API contains a nice little enhancement. Get Lead Activities now accepts the leadIds parameter that can be used to specify a lead id. When the leadIds parameter is specified, only activities for that lead id will be returned. You can think of this as a lead id filter.
Note that the leadIds parameter can take a comma separated list of lead ids in case you’d like to filter results on more than one lead (up to 30). This might come in handy for example when limiting activities to leads for a particular company.
Example
Below is a sample request to Get Lead Activities that contains the leadIds parameter. I have specified a value of “50” for the leadIds parameter, which corresponds to an arbitrary lead in my Marketo instance. I have specified a value of “129” for the activityTypeIds parameter, which corresponds to the “Mobile App Session” activity on my Marketo instance.
1 |
https://123-abc-456.mktorest.com/rest/v1/activities.json?leadIds=50&activityTypeIds=129&nextPageToken=WQV2VQVPPCKHC6AQYVK7JDSA3J4SMAZRQO4RKIXCEMLFCM2APRSQ==== |
Below is an excerpt of the response from that request above. As you can see, it contains only result objects with “leadId”: 50 and “activityTypeId”: 129.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
{ "id": 846, "leadId": 50, "activityDate": "2015-04-06T21:58:59Z", "activityTypeId": 129, "primaryAttributeValueId": 13, "primaryAttributeValue": "Sample App", "attributes": [ { "name": "Device Type", "value": "iPhone" }, { "name": "Mobile App Session Length", "value": 7 }, { "name": "Platform", "value": "ios" } ] } { "id": 879, "leadId": 50, "activityDate": "2015-04-07T00:45:11Z", "activityTypeId": 129, "primaryAttributeValueId": 13, "primaryAttributeValue": "Sample App", "attributes": [ { "name": "Device Type", "value": "iPhone" }, { "name": "Mobile App Session Length", "value": 5 }, { "name": "Platform", "value": "ios" } ] } { "id": 1114, "leadId": 50, "activityDate": "2015-04-08T00:02:41Z", "activityTypeId": 129, "primaryAttributeValueId": 13, "primaryAttributeValue": "Sample App", "attributes": [ { "name": "Device Type", "value": "iPhone" }, { "name": "Mobile App Session Length", "value": 241 }, { "name": "Platform", "value": "ios" } ] } { "id": 1551, "leadId": 50, "activityDate": "2015-04-09T23:31:56Z", "activityTypeId": 129, "primaryAttributeValueId": 13, "primaryAttributeValue": "Sample App", "attributes": [ { "name": "Device Type", "value": "iPhone" }, { "name": "Mobile App Session Length", "value": 223 }, { "name": "Platform", "value": "ios" } ] } { "id": 1716, "leadId": 50, "activityDate": "2015-04-15T22:44:19Z", "activityTypeId": 129, "primaryAttributeValueId": 13, "primaryAttributeValue": "Sample App", "attributes": [ { "name": "Device Type", "value": "iPhone" }, { "name": "Mobile App Session Length", "value": 223 }, { "name": "Platform", "value": "ios" } ] } |
Fun With Shell Scripts
While working on this post, I wrote the following shell script to automate lead activity retrieval. If I’ve written it correctly, it should be self explanatory 😉 Feel free to replace the constants at the top of the script with values from your instance and give it a try.
Note that this script makes use of a command line tool “jq”. I’ve found that jq and jq play are indispensable tools when slicing and dicing JSON response bodies. Enjoy!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
#!/usr/bin/bash # # # Copy these values from Admin > Integration > Web Services > REST API readonly REST_ENDPOINT='https://123-abc-456.mktorest.com/rest' readonly REST_IDENTITY='https://123-abc-456.mktorest.com/identity' # Copy these values from Admin > Integration > LaunchPoint > Edit Service readonly CLIENT_ID='12345678-1234-1234-1234-123456789012' readonly CLIENT_SECRET='11122233344455566677788899900011' # Lead id of interest readonly LEAD_ID='50' # Activity name of interest readonly ACTIVITY_NAME='Mobile App Session' # Date that determines how far back in time to retrieve activities readonly SINCE_DATETIME='2015-01-01' ########################## # Retrieve access token # # Globals: # REST_IDENTITY # # Arguments: # 1. Client id # 2. Client secret # # Returns: # Access token ########################## function getAccessToken() { local client_id=$1 local client_secret=$2 # Call Identity API local result=`curl "${REST_IDENTITY}/oauth/token?grant_type=client_credentials&client_id=${client_id}&client_secret=${client_secret}"` # Extract access token from result local access_token=`echo ${result} | jq .access_token` access_token=`echo ${access_token} | tr -d '"'` echo $access_token } ########################## # Retrieve activity id # # Globals: # REST_ENDPOINT # # Arguments: # 1. Access token # 2. Activity name # # Returns: # Activity id ########################## function getActivityId() { local access_token=$1 local activity_name="$2" # Call Get Activity Types API local result=`curl "${REST_ENDPOINT}/v1/activities/types.json?access_token=${access_token}"` # Extract activity id local activity_id=`echo ${result} | jq -c ".result[] | select(.name | contains("${activity_name}"))"` activity_id=`echo ${activity_id} | jq .id` echo $activity_id } ########################## # Retrieve page token # # Globals: # REST_ENDPOINT # # Arguments: # 1. Access token # 2. Since datetime # # Returns: # Page token ########################## function getPageToken() { local access_token=$1 local since_datetime=$2 # Call Get Paging Token API local result=`curl "${REST_ENDPOINT}/v1/activities/pagingtoken.json?access_token=${access_token}&sinceDatetime=${since_datetime}"` # Extract page token local page_token=`echo ${result} | jq .nextPageToken` page_token=`echo ${page_token} | tr -d '"'` echo $page_token } ########################## # Retrieve activity dates for given lead(s) for a given activity # # Globals: # REST_ENDPOINT # # Arguments: # 1. Access token # 2. Page token # 3. Activity id # 4. Lead id(s) # # Returns: # None ########################## function getLeadActivities() { local access_token=$1 local page_token=$2 local activity_id=$3 local lead_ids=$4 local more_result="true" local lead_activities="" local success="true" local lead_activities="" while [ ${more_result} = "true" ] do # Call Get Lead Activities API local result=`curl "${REST_ENDPOINT}/v1/activities.json?access_token=${access_token}&nextPageToken=${page_token}&activityTypeIds=${activity_id}&leadIds=${lead_ids}"` # Extract success, nextPageToken, moreResult success=`echo ${result} | jq .success` page_token=`echo ${result} | jq .nextPageToken` page_token=`echo ${page_token} | tr -d '"'` more_result=`echo ${result} | jq .moreResult` # Extract activity dates lead_activities+=`echo ${result} | jq .result[]` done echo $lead_activities } readonly ACCESS_TOKEN=$(getAccessToken "${CLIENT_ID}" "${CLIENT_SECRET}") readonly ACTIVITY_ID=$(getActivityId "${ACCESS_TOKEN}" "${ACTIVITY_NAME}") readonly PAGE_TOKEN=$(getPageToken "${ACCESS_TOKEN}" "${SINCE_DATETIME}") readonly ALL_LEAD_ACTIVITIES=$(getLeadActivities "${ACCESS_TOKEN}" "${PAGE_TOKEN}" "${ACTIVITY_ID}" "${LEAD_ID}") echo "ALL_LEAD_ACTIVITIES=${ALL_LEAD_ACTIVITIES}" |