[PYTHON] Get conversions and revenue with Google Analytics API and report to Slack

We are measuring site conversions with Google Analytics.

スクリーンショット 2017-09-15 1.58.32.png

Get conversion numbers with API

I used the Google Analytics API to automatically get conversions and revenue values.

python


  response = analytics.reports().batchGet(
      body={
        'reportRequests': [
        { 'viewId':     view_id,
          'dateRanges': [{'startDate': start_date, 'endDate': end_date}],
          'metrics': [{'expression': 'ga:transactions'}, {'expression': 'ga:transactionRevenue'}]
        }]
      }
  ).execute()
  values = response.get['reports'][0]['data']['totals'][0]['values']
  transactions       = values[0]
  transactionRevenue = values[1]

I searched for metrics in the reference manual.

Notify slack

I used slack web.

  slack = slackweb.Slack(url=slack_webhook_url))
  slack.notify(text=text)

It's easy.

Where it takes time

For the first time, there are lots of things other than code, knowledge about creating Google service account keys, knowledge of Google Analytics configuration and user management, key type P12 or JSON, Slack webhook settings, etc. I hope you have time to spare. By comparison, the code is less difficult.

reference

Dimensions & Metrics Explorer | Analytics Reporting API v4 | Google Developers https://developers.google.com/analytics/devguides/reporting/core/dimsmets

Recommended Posts

Get conversions and revenue with Google Analytics API and report to Slack
Get data from analytics API with Google API Client for python
Get holidays with the Google Calendar API
Challenge to create time axis list report with Toggl API and Python
Get tweets with Google Cloud Function and automatically save images to Google Photos
Display Google Maps API with Rails and pin display
Get Gmail subject and body with Python and Gmail API
[Python] Mention to multiple people with Slack API
How to use Service Account OAuth and API with Google API Client for python
[First API] Try to get Qiita articles with Python
Book registration easily with Google Books API and Rails
Get comments and subscribers with the YouTube Data API
How to analyze with Google Colaboratory using Kaggle API
[Python] Get user information and article information with Qiita API
Upload to a shared drive with Google Drive API V3
Easy to use Nifty Cloud API with botocore and python
Send experiment results (text and images) to slack with Python
Streamline information gathering with the Twitter API and Slack bots
Get a global IP and export it to Google Spreadsheets
[Python x Zapier] Get alert information and notify with Slack
Get additional data to LDAP with python (Writer and Reader)
Upload and delete files to Google Cloud Storages with django-storage
Post to slack with Python 3
Get information with zabbix api
Get ranking with Rakuten API
I tried to visualize bookmarks flying to Slack with Doc2Vec and PCA
How to get followers and followers from python using the Mastodon API
Correspondence analysis of sentences with COTOHA API and save to file
Get Keyword Planner Search Volume / CPC / Competitiveness with Google Ads API
Try to display google map and geospatial information authority map with python
The strongest way to use MeCab and CaboCha with Google Colab
Link to get started with python
Get reviews with python googlemap api
Export Google Analytics Standard to BigQuery
How to get started with Scrapy
How to get started with Python
Get and visualize google search trends
How to get started with Django
Introduce errBot and work with Slack
Selenium and python to open google
Introducing Google Map API with rails
Use Google Analytics API from Python
Google API access token and refresh token
[Rails] google maps api How to post and display including map information
How to get the date and time difference in seconds with python
Wrap reading and writing of GCP to Secret Manager with google subcommands
I tried to get the authentication code of Qiita API with Python.
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
How to get a sample report from a hash value using VirusTotal's API
POST photos with Microsoft Bing Image Search API to get Image Insights (Python)
Get the number of articles accessed and likes with Qiita API + Python
I tried to get the movie information of TMDb API with Python
Edit Slide (PowerPoint for Google) with Python (Low-cost RPA case with Google API and Python)
Don't use your username and password to register with PyPI. Use API tokens
Feel free to knock 100 data sciences with Google Colab and Azure Notebooks!
How to search using python's astroquery and get fits images with skyview
[Rails] How to calculate latitude and longitude with high accuracy using Geocoding API and display it on Google Map