adobe-analytics-api-python/docs/getting_started.md at master · vikash-kothary/adobe-analytics-api-python

Getting Started with the python wrapper for Adobe Analytics API 2.0

On this page, a quick example on how to start with the wrapper. More details explanation are available in the main file or one the datanalyst.info website

1. Create an Adobe IO console account

First you should create an Adobe IO account and connect to a Product Profile. You can also create an Adobe IO account and then go to the product Profile in Adobe Admin Console to contect to your Adobe IO account.

When you create your Adobe IO account, you need to set a certificate, keep the key nearby because you will need it. You can follow this tutorial

2. Download the library

You can directly install the library from the command line:

or

python -m pip install --upgrade git+<https://github.com/pitchmuc/adobe_analytics_api_2.0.git#egg=aanalytics2>

or you can copy paste the aanalytics2.py file in your Lib/ folder in your Python installation.

3. Setup a JSON with your information

Starting with the wrapper, you can import it and create a template for the JSON file that will store your credential to your Adobe IO account.

import aanalytics2 as api2
api2.createConfigFile()

This will create a JSON and you will need to fill it with the information available in your adobe io account.

NOTE: starting version 0.4.0 the createConfigFile will by default create a Oauth Server to Server configuration file. You can always use the argument to change the behavior.

Arguments for createConfigFile:

  • destination : OPTIONAL : the name of the file + path if you want
  • auth_type : OPTIONAL : The type of Oauth type you want to use for your config file. Possible value: "jwt" or "oauthV2"

4. Import the configuration file

Once this is done, you can import the configuration file. I would recommend to store the config file and the key in the folder that you are using, however, the element will work if you are using correct path.

import aanalytics2 as api2
api2.importConfigFile('myconfig.json')

Note: starting version 0.4.0 the importConfigFile will check dynamically for the keys used to define the type of Auth. The check is Oauth V2 and then JWT. You can force the usage of a specific setup by passing the type of auth you want to priorize.

Alternative 1 : Using the configure method

When you want to connect to the Analytics API from a server application, you may want to use the configure method and passing the element directly. You can see more details on that connection method on the authentication without config json page

Alternative 2 : Using Oauth Token

It may be the case that you only have information from Oauth authentication.
In that case, you usually ends up with the following information:

  • orgId
  • clientId
  • token

In that case, you can use the configure method as well with some specific parameters.

  • oauth
  • token
import aanalytics2 as api2

api2.configure(oauth=True,org_id='XXXXXX@AdobeOrg',client_id='ysfn28d938z2een27z4',token='myToken')

NOTE: The token will be valid for 22 hours and will not be refresh afterwards.

5. Get Company ID(s) & create your instance

Once all of these setup steps are completed, you can instantiate the Login class.
Starting version 0.1.1 a new class (Login) is avalabile in order to retrieve the login company.
This is the default path that you should use to retrieve your companyId.

import aanalytics2 as api2
api2.importConfigFile('myconfig.json')

login = api2.Login()
cids = login.getCompanyId()

#you can also access the login return through the instance
login.COMPANY_IDS
## returns the same result that cids.

From there, 2 methods can be used to create the Analytics class instance.

mycompany = api2.Analytics(cid)

# method directly in the Login class
mycompany = login.createAnalyticsConnection(cid)

Note that for both methods you can use the retry parameter to set the number of times the request can be retry in case of Adobe Analytics server not responding.
This option will be set for every GET method in your instance.

mycompany = api2.Analytics(cid, retry=3)

# new method directly in the Login class
mycompany = login.createAnalyticsConnection(cid, retry=3)

Multi-credential setup (several API keys in parallel)

If you need to connect to Adobe Analytics with different credentials (e.g. different client IDs / API keys) at the same time, use return_object=True when importing each config file.
This returns an isolated ConfigObj instance that carries its own copy of the credentials and header, with no shared memory between instances.

import aanalytics2 as api2
from concurrent.futures import ThreadPoolExecutor

# Load each credential file into an independent ConfigObj
cfg1 = api2.importConfigFile('creds_client1.json', return_object=True)
cfg2 = api2.importConfigFile('creds_client2.json', return_object=True)

# Option A — via Login (use when you need to call getCompanyId first)
login1 = api2.Login(**cfg1)
login2 = api2.Login(**cfg2)
company1 = login1.createAnalyticsConnection('company_A')
company2 = login2.createAnalyticsConnection('company_B')

# Option B — directly to Analytics (use when you already know the companyId)
company1 = api2.Analytics(company_id='company_A', **cfg1)
company2 = api2.Analytics(company_id='company_B', **cfg2)

# Both instances can be used concurrently — each manages its own token lifecycle
with ThreadPoolExecutor() as pool:
    f1 = pool.submit(company1.getSegments)
    f2 = pool.submit(company2.getSegments)
    segments1 = f1.result()
    segments2 = f2.result()

Note: the classic single-credential flow (importConfigFileLogin()Analytics(cid)) is unchanged and fully backward-compatible.

6. Use the methods in your instance

Once you have the instance created, you can use the different methods available to them. Don't hesitate to use the help() function in order to have more details on the different possible parameters. Example :

segments = mycompany.getSegments()

or

myreport = mycompany.getReport('myRequest.json')

The response that is given is a dictionary with the relevant information from your request (timeframe, segments used, etc...)
You can access the data directly with the "data" keyword.