Search code examples
pythongoogle-analytics-api

Google Analytics MCF API


Im trying to use the below class to help build API calls to google analytics.

 apiclient.discovery import build

It looks pretty plug and play so I figured I would tyr and use the same thing when I want to call the MultiChannelFunnel api. I altered the below code to say 'mcf', 'analytics#mcfData' as well as a couple of other iterations, instead of "analyticsreporting" (as thats what it is for the core reporting).

def initialize_analyticsreporting():
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      KEY_FILE_LOCATION, SCOPES)

  analytics = build('mcf', 'v3', credentials=credentials)
  return analytics

And of course Im getting errors about bad names. I cant seem to find anywhere in the documentation what the actual service name is (the documentation for build() says the param name is service name, here is a link: https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.discovery-pysrc.html#build )

So my questions are:

  1. Does anyone know the service name for the mcf api from google analytics?
  2. Do I have to try a different approach for calling this api? I suspect this might sadly be the case.

Solution

  • Multi channel funnel reports are a sub-service of the Analytics API. So you can just do something along these lines:

    analytics_api = build('analytics', 'v3', credentials=credentials)
    mcf_service = analytics_api.data().mcf()
    
    start_date = str(datetime.date(2022, 1, 1))
    end_date = str(datetime.date.today())
    
    report = mcf_service.get(
        start_date=start_date,
        end_date=end_date,
        metrics='mcf:totalConversions,mcf:totalConversionValue',
        ids='ga:12345'
    ).execute()