3. Basic example¶
In this simple example we will see how to use the main functions available in the toolkit to collect basic data from the LinkedIn API, namely how many women have an account in Nigeria.
Before starting, make sure to have copied the output of the “Obtaining headers and cookies” sections in the file called config.py, which will be in the same directory as the functions’ one. Also do not change the variables names’, simply modify their values. Your config file should look like this:
accountId = '123456789'
cookies = {
'JSESSIONID': 'ajax:3444899384839734769',
'lang': 'v=2&lang=en-us',
'bcookie': 'v=2&10bb703a-0fa6-4760-816c-aacf404564bc',
'bscookie': 'v=1&20210223215620e26760f6-c981-47b7-8bc3-d355557ddb7eAQFM2da0hDkXkQ8xInuD-NaJVYX4PK6k',
'lidc': 'b=OB29:s=O:r=O:g=3106:u=199:i=1614117445:t=1614119167:v=1:sig=AQEgovCm8qrTDgE2HC1RXimTR2QyTzMZ',
'G_ENABLED_IDPS': 'google',
'_ga': 'GA1.2.1791209285.1614117381',
'_gid': 'GA1.2.761002408.1614117381',
'AMCV_14215E3D5995C57C0A495C55%40AdobeOrg': '-637568504%7CMCIDTS%7C18682%7CMCMID%7C39046611493886834864521875890884764148%7CMCAAMLH-1614722192%7C4%7CMCAAMB-1614722192%7C6G1ynYcLPuiQxYZrsz_pkqfLG9yMXBpb2zX5dvJdYQJzPXImdj0y%7CMCOPTOUT-1614124592s%7CNONE%7CvVersion%7C5.1.1%7CMCCIDH%7C1843510176',
'AMCVS_14215E3D5995C57C0A495C55%40AdobeOrg': '1',
'aam_uuid': '39628989621111549654472005257624556095',
'liap': 'true',
'li_at': 'AQEDAS0hsW0FUk1kAAABd9Dkqa0AAAF39PEtrU4Awtraw8aRRpAk3ACenprZdyrXUQT9ndSnE5HTYcN6HsKRV3GQRDV3_SZkp8T9kaI4wJRseTCII1KL5zQfSUBMABLdVIJ5ytGGkL0LG2yM4_Ty-piw',
'timezone': 'America/Sao_Paulo',
'_gcl_au': '1.1.857222471.1614117443',
'li_sugr': '9e35a69e-cad2-47ca-b933-0c32bdb694e5',
'UserMatchHistory': 'AQIYLSPKtTosjwAAAXfQ5ZlBLv05siLkgmsAJasL3sA16OA8Wzv1dQoHOK5OLDAzxkvuC3361IycZA',
'AnalyticsSyncHistory': 'AQK4UDr19qZ_YAAAAXfQ5ZlB3H2goLKaRXo2KZ7mSkhAWO_DqZ5UU06erb7Gw9kkzHsVfSp5gS_a9NgdqoWMmw',
'lms_ads': 'AQGFNEwxqScvpQAAAXfQ5ZqAJTVJE3SRpIoRYjRdbCMceHFQUW49aa2kKJCQRvpbyZEbmwCqiFsYEg4tu4lkm9zKHNfcoGU2',
'lms_analytics': 'AQGFNEwxqScvpQAAAXfQ5ZqAJTVJE3SRpIoRYjRdbCMceHFQUW49aa2kKJCQRvpbyZEbmwCqiFsYEg4tu4lkm9zKHNfcoGU2',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:85.0) Gecko/20100101 Firefox/85.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'x-restli-protocol-version': '2.0.0',
'x-li-lang': 'en_US',
'x-li-track': '{"clientVersion":"2.0.440","mpVersion":"2.0.440","osName":"web","timezoneOffset":-3,"deviceFormFactor":"DESKTOP","mpName":"campaign-manager-web","displayDensity":2,"displayWidth":3072,"displayHeight":1920}',
'x-li-page-instance': 'urn:li:page:d_campaign_details;YpTIDnw6S/u4XPAdqaW/tQ==',
'csrf-token': 'ajax:3444899384839734769',
'x-li-er-key': 'urn:li:sponsoredAccount:503845487',
'x-http-method-override': 'GET',
'content-type': 'application/x-www-form-urlencoded',
'Origin': 'https://www.linkedin.com',
'Connection': 'keep-alive',
'Referer': 'https://www.linkedin.com/campaignmanager/accounts/503845487/campaigns/new/details?campaignGroupId=611166973',
'TE': 'Trailers',
}
testheaders = """POST /campaign-manager-api/campaignManagerAudienceCounts HTTP/1.1
Host: www.linkedin.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:85.0) Gecko/20100101 Firefox/85.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
x-restli-protocol-version: 2.0.0
x-li-lang: en_US
x-li-track: {"clientVersion":"2.0.440","mpVersion":"2.0.440","osName":"web","timezoneOffset":-3,"deviceFormFactor":"DESKTOP","mpName":"campaign-manager-web","displayDensity":2,"displayWidth":3072,"displayHeight":1920}
x-li-page-instance: urn:li:page:d_campaign_details;YpTIDnw6S/u4XPAdqaW/tQ==
csrf-token: ajax:3444899384839734769
x-li-er-key: urn:li:sponsoredAccount:503845487
x-http-method-override: GET
content-type: application/x-www-form-urlencoded
Content-Length: 645
Origin: https://www.linkedin.com
Connection: keep-alive
Referer: https://www.linkedin.com/campaignmanager/accounts/503845487/campaigns/new/details?campaignGroupId=611166973
Cookie: JSESSIONID="ajax:3444899384839734769"; lang=v=2&lang=en-us; bcookie="v=2&10bb703a-0fa6-4760-816c-aacf404564bc"; bscookie="v=1&20210223215620e26760f6-c981-47b7-8bc3-d355557ddb7eAQFM2da0hDkXkQ8xInuD-NaJVYX4PK6k"; lidc="b=OB29:s=O:r=O:g=3106:u=199:i=1614117445:t=1614119167:v=1:sig=AQEgovCm8qrTDgE2HC1RXimTR2QyTzMZ"; G_ENABLED_IDPS=google; _ga=GA1.2.1791209285.1614117381; _gid=GA1.2.761002408.1614117381; AMCV_14215E3D5995C57C0A495C55%40AdobeOrg=-637568504%7CMCIDTS%7C18682%7CMCMID%7C39046611493886834864521875890884764148%7CMCAAMLH-1614722192%7C4%7CMCAAMB-1614722192%7C6G1ynYcLPuiQxYZrsz_pkqfLG9yMXBpb2zX5dvJdYQJzPXImdj0y%7CMCOPTOUT-1614124592s%7CNONE%7CvVersion%7C5.1.1%7CMCCIDH%7C1843510176; AMCVS_14215E3D5995C57C0A495C55%40AdobeOrg=1; aam_uuid=39628989621111549654472005257624556095; liap=true; li_at=AQEDAS0hsW0FUk1kAAABd9Dkqa0AAAF39PEtrU4Awtraw8aRRpAk3ACenprZdyrXUQT9ndSnE5HTYcN6HsKRV3GQRDV3_SZkp8T9kaI4wJRseTCII1KL5zQfSUBMABLdVIJ5ytGGkL0LG2yM4_Ty-piw; timezone=America/Sao_Paulo; _gcl_au=1.1.857222471.1614117443; li_sugr=9e35a69e-cad2-47ca-b933-0c32bdb694e5; UserMatchHistory=AQIYLSPKtTosjwAAAXfQ5ZlBLv05siLkgmsAJasL3sA16OA8Wzv1dQoHOK5OLDAzxkvuC3361IycZA; AnalyticsSyncHistory=AQK4UDr19qZ_YAAAAXfQ5ZlB3H2goLKaRXo2KZ7mSkhAWO_DqZ5UU06erb7Gw9kkzHsVfSp5gS_a9NgdqoWMmw; lms_ads=AQGFNEwxqScvpQAAAXfQ5ZqAJTVJE3SRpIoRYjRdbCMceHFQUW49aa2kKJCQRvpbyZEbmwCqiFsYEg4tu4lkm9zKHNfcoGU2; lms_analytics=AQGFNEwxqScvpQAAAXfQ5ZqAJTVJE3SRpIoRYjRdbCMceHFQUW49aa2kKJCQRvpbyZEbmwCqiFsYEg4tu4lkm9zKHNfcoGU2
"""
If that’s the case, you are ready to import the functions and make your first request through the API.
from linkedin_functions import *
We will delve into the functions’ structure and capabilities in later notebooks, but for now we will identify two main stages in the data collection: 1) the creation of the URLs that will be used to communicate to the API what data we want, and 2) the GET HTTP requests themselves.
To create the URLs, we will use the function “createRequestDataForAudienceCounts”: its arguments are the characteristics of the segment that we want to query, like its education or sector, and must be provided as strings in a list. It is always necessary to specify the desired location, while the other arguments are optional and default to ALL, i.e. all possible values.
It is important to note that while the API often recognises slight spelling differences/mistakes - “Nigeria”, “nigeria” and “Nigeri” will all work and return the same result - this is not guaranteed to happen, and it is thus always recommended to check the right wording and available options on LinkedIn’s webpage of your campaign.
requestCriteria = createRequestDataForAudienceCounts(locations = ['Nigeria'],
genders = ['Female'])
For a simple request like this, the URL will look like
requestCriteria
'q=targetingCriteria&cmTargetingCriteria=(include:(and:List((or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3Alocations,name:Locations),segments:List((urn:urn%3Ali%3Ageo%3A105365761,name:Nigeria,facetUrn:urn%3Ali%3AadTargetingFacet%3Alocations))))),(or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3Agenders,name:MemberGender),segments:List((urn:urn%3Ali%3Agender%3AFEMALE,name:Female,facetUrn:urn%3Ali%3AadTargetingFacet%3Agenders))))),(or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3AageRanges,name:MemberAge),segments:List()))),(or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3Adegrees,name:Degrees),segments:List()))),(or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3AfieldsOfStudy,name:FieldsofStudy),segments:List()))),(or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3Aseniorities,name:JobSeniorities),segments:List()))),(or:List((facet:(urn:urn%3Ali%3AadTargetingFacet%3Aindustries,name:CompanyIndustries),segments:List()))))),exclude:(or:List()))&withValidation=true'
Finally, we can get the number of Nigerian women with a LinkedIn account by using the getAudienceCounts function, which returns the desired number. In this case, that is 1.3M.
getAudienceCounts(requestCriteria)
1300000