Search code examples
pythonlinkedin-api

Reporting Endpoints in LinkedIn Marketing API


I've been trying to hit the new "versioned" LinkedIn Marketing API in an attempt to pull any kind of analytics on my campaigns. Utilizing the python and requests. Documentation found here.

import requests

headers = {
   'X-Restli-Protocol-Version': '2.0.0',
   'Authorization': 'Bearer {TOKEN}',
   'Linkedin-Version': '202303',
}

url = 'https://api.linkedin.com/rest/adAnalytics?q=statistics&pivot=CAMPAIGN&dateRange.start.day=1&dateRange.start.month=3&dateRange.start.year=2023&timeGranularity=DAILY&fields=pivotValue&accounts=urn%3Ali%3AsponsoredAccount%3A507920541'

response = requests.get(url, headers=headers)
print(response)

After a bunch of fighting, I've settled that my closest effort yields this result:

{'errorDetailType': 'com.linkedin.common.error.BadRequest', 'message': 'Multiple errors occurred during query param validation. Please see errorDetails for more information.', 'errorDetails': {'inputErrors': [{'input': {'inputPath': {'fieldPath': 'dateRange.start.day'}}, 'code': 'QUERY_PARAM_NOT_ALLOWED'}, {'input': {'inputPath': {'fieldPath': 'dateRange.start.month'}}, 'code': 'QUERY_PARAM_NOT_ALLOWED'}, {'input': {'inputPath': {'fieldPath': 'dateRange.start.year'}}, 'code': 'QUERY_PARAM_NOT_ALLOWED'}]}, 'status': 400}

I do not know what it is looking for for the date range. If change the Linkedin-Version to 202302 I get this error:

{'status': 403, 'serviceErrorCode': 100, 'code': 'ACCESS_DENIED', 'message': 'Unpermitted fields present in PARAMETER: Data Processing Exception while processing fields [/dateRange.start.day, /dateRange.start.month, /dateRange.start.year]'}

Suggestions are very much appreciated.

EDIT

Utilizing postman as was suggested in the comments yielded this:

{ "status": 400, "code": "ILLEGAL_ARGUMENT", "message": "Invalid query parameters passed to request" }

This was the url, which I think is set up as the old version tbh.

https://api.linkedin.com/rest/adAnalytics?q=statistics&dateRange.start.year=2023&dateRange.start.month=3&dateRange.start.day=1&dateRange.end.year=2023&dateRange.end.month=03&dateRange.end.day=20&timeGranularity=ALL&accounts=urn:li:sponsoredAccount:507920541&pivots[0]=CAMPAIGN&pivots[1]=CONVERSION&fields=externalWebsiteConversions,externalWebsitePostClickConversions,externalWebsitePostViewConversions,costInLocalCurrency,externalWebsiteConversions,costInLocalCurrency,dateRange,pivotValues


Solution

  • Where it landed is something like this:

    https://api.linkedin.com/rest/adAnalytics?q=statistics&dateRange.start.year=2023&dateRange.start.month=1&dateRange.start.day=1&dateRange.end.year=2023&dateRange.end.month=04&dateRange.end.day=17&timeGranularity.value=DAILY&accounts=urn%3Ali%3AsponsoredAccount%3AXXXXXXXXX&pivots[0].value=CAMPAIGN&projection[0].value=pivotValues&fields=pivotValues,impressions"

    As the URL but I needed to use the 1.0.0 X-Restli-Protocol-Version header (Which might be default). They do not make that clear in their documentation. I also recommend this page:

    https://learn.microsoft.com/en-us/linkedin/shared/api-guide/concepts/protocol-version

    Which cleared some things up but is not linked from the documentation.