Setup Prerequisites
- Create an IBM Cloud account if you don't have one.
- Ensure you have access to a Google Analytics account with the property you want to integrate with IBM Watson.
- Set up an API key for Google Analytics and IBM Watson. Both services require authentication using their API keys.
Access IBM Watson and Google Analytics APIs
- Navigate to IBM Cloud Dashboard and create an instance of IBM Watson that you intend to use, e.g., Watson Assistant, Natural Language Understanding, etc.
- Go to your Google Cloud Platform, select the appropriate project, and enable the Google Analytics API under “APIs & Services.”
Install Necessary Libraries
- To interact with these services programmatically, you can use Python. First, ensure Python is installed on your system.
- Install the `ibm-watson` and `google-api-python-client` libraries:
pip install ibm-watson google-api-python-client
Authenticate with IBM Watson
- Save the Watson API key and your instance URL.
- Use the following code snippet to authenticate with IBM Watson:
from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('your-ibm-watson-api-key')
assistant = AssistantV2(
version='2021-06-14',
authenticator=authenticator
)
assistant.set_service_url('your-ibm-watson-url')
Authenticate with Google Analytics
- Save the credentials to a JSON file recommended by Google when you create an API key. Use this file for authentication.
- Use the snippet below to authenticate:
from google.oauth2 import service_account
from googleapiclient.discovery import build
credentials = service_account.Credentials.from_service_account_file(
'path/to/your/credentials.json',
scopes=['https://www.googleapis.com/auth/analytics.readonly']
)
analytics = build('analyticsreporting', 'v4', credentials=credentials)
Fetch Data from Google Analytics
- Query Google Analytics to fetch the data you need:
- Replace the placeholder values with actual values that suit your needs:
response = analytics.reports().batchGet(
body={
'reportRequests': [
{
'viewId': 'your-view-id',
'dateRanges': [{'startDate': '7daysAgo', 'endDate': 'today'}],
'metrics': [{'expression': 'ga:sessions'}]
}]
}
).execute()
Process Data using IBM Watson
- Send this data to IBM Watson for processing. Here's an example that forwards data to Watson Assistant:
session_response = assistant.create_session(
assistant_id='your-assistant-id'
).get_result()
session_id = session_response['session_id']
message_response = assistant.message(
assistant_id='your-assistant-id',
session_id=session_id,
input={
'message_type': 'text',
'text': 'User had {} sessions this week'.format(response['reports'][0]['data']['totals'][0]['values'][0])
}
).get_result()
print(message_response['output']['generic'][0]['text'])
Considerations for Integration
- Data Volume: Be mindful of API limits and perform data processing in batches if necessary.
- Security: Ensure you store your API keys securely and rotate them periodically.
- Error Handling: Include error handling to manage potential API failures gracefully.
Test Your Integration
- After setting up both services, run the complete code to ensure data flows as expected.
- Check both your Google Analytics reports and IBM Watson response to confirm successful integration.