You can authenticate with Google to create your own instance of the Google Analytics element through the UI or through APIs. Once authenticated, you can use the element instance to access the different functionality offered by the Google Analytics platform.
Authenticate Through the UI
Use the UI to authenticate with Google and create an element instance. Because you authenticate with Google via OAuth 2.0, all you need to do is add a name for the instance. After you create the instance, you'll log in to Google Analytics to authorize Cloud Elements access to your account. For more information about authenticating an element instance, see Authenticate an Element Instance (UI).
After successfully authenticating, we give you several options for next steps. Make requests using the API docs associated with the instance, map the instance to a virtual data resource, or use it in a formula template.
Authenticate Through API
Authenticating through API is a multi-step process that involves:
- Getting a redirect URL. This URL sends users to the vendor to log in to their account.
- Authenticating users and receiving the authorization grant code. After the user logs in, the vendor makes a callback to the specified url with an authorization grant code.
- Authenticating the element instance. Using the authorization code from the vendor, authenticate with the vendor to create an element instance at Cloud Elements.
Getting a Redirect URL
Use the following API call to request a redirect URL where the user can authenticate with the vendor. Replace {keyOrId}
with the element key, googleanalytics
.
GET /elements/{keyOrId}/oauth/url?apiKey=<api_key>&apiSecret=<api_secret>&callbackUrl=<url>&siteAddress=<url>
Query Parameters
Query Parameter | Description |
---|---|
apiKey | The key obtained from registering your app with the provider. This is the App key that you recorded in Google Analytics API Provider Setup. |
apiSecret | The client secret obtained from registering your app with the provider. This is the App secret that you recorded in Google Analytics API Provider Setup. |
callbackUrl | The URL that will receive the code from the vendor to be used to create an element instance. This is https://auth.cloudelements.io/oauth, the redirect URL you added to your Google Analytics app during Google Analytics API Provider Setup. |
Example cURL
curl -X GET
-H 'Content-Type: application/json'
'https://api.cloud-elements.com/elements/api-v2/elements/googleanalytics/oauth/url?apiKey=<INSERT_API_KEY>&apiSecret=fake_api_secret&callbackUrl=https://auth.cloudelements.io/oauth&state=googleanalytics'
Example Response
Use the oauthUrl
in the response to allow users to authenticate with the vendor.
{
"oauthUrl": "https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&prompt=consent&scope=+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.manage.users.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.manage.users+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.edit+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics&response_type=code&redirect_uri=https%3A%2F%2Fauth.cloudelements.io%2Foauth&state=googleanalytics&client_id=",
"element": "googleanalytics"
}
Authenticating users and receiving the authorization grant code
Provide the response from the previous step to the users. After they authenticate, Google Analytics provides the following information in the response:
- code
- state
Response Parameter | Description |
---|---|
code | The Authorization Grant Code required by Cloud Elements to retrieve the OAuth access and refresh tokens from the endpoint. |
state | A customizable identifier, typically the element key (googleanalytics ) . |
error
instead of the code
parameter. In this case, your application can handle the error gracefully.Authenticating the Element Instance
Use the /instances
endpoint to authenticate with Google Analytics and create an element instance. If you are configuring events, see Google Analytics Events.
To create an element instance:
Construct a JSON body as shown below (see Parameters):
{ "element": { "key": "googleanalytics" }, "providerData": { "code": "<AUTHORIZATION_GRANT_CODE>" }, "configuration": { "account.id": "<ACCOUNT_ID>", "account.username": "<ACCOUNT_NAME>", "oauth.api.key": "<CLIENT_ID>", "oauth.api.secret": "<CLIENT_SECRET>", "oauth.callback.url": "https://www.mycoolapp.com/auth" }, "tags": [ "<ADD_YOUR_TAGS>" ], "name": "<INSTANCE_NAME>" }
Call the following, including the JSON body you constructed in the previous step:
POST /instances
Note: Make sure that you include the User and Organization keys in the header. For more information, see Authorization Headers, Organization Secret, and User Secret.Note the Token and ID and save them for all future requests using the element instance.
Example cURL
curl -X POST \
https://api.cloud-elements.com/elements/api-v2/instances \
-H 'authorization: User <USER_SECRET>, Organization <ORGANIZATION_SECRET>' \
-H 'content-type: application/json' \
-d '
{
"element": {
"key": "googleanalytics"
},
"providerData": {
"code": "12p8hsa8ehgpishef9yeg89ashegaspe"
},
"configuration": {
"account.id": "3038675309",
"account.username": "My Cool Project",
"oauth.api.key": "a8hg9sa08g983htui3h8hsfd",
"oauth.api.secret": "aeiosfh9a8f9083hf98q3g98sgr980ashe9f",
"oauth.callback.url": "https://www.mycoolapp.com/auth"
},
"tags": [
"Analytics"
],
"name": "API Instance"
}
'
Parameters
API parameters not shown in Cloud Elements are in code formatting
.
Parameter | Description | Data Type |
---|---|---|
'key' | The element key. googleanalytics | string |
code | The authorization grant code returned from the API provider in an OAuth 2.0 authentication workflow. Cloud Elements uses the code to retrieve the OAuth access and refresh tokens from the endpoint. | string |
Namename | The name for the element instance created during authentication. | Body |
account.id | The numeric ID of the Analytics project with which you want to authenticate. This is the account ID you recorded in Google Analytics API Provider Setup. | string |
account.username | The name of the the Analytics project with which you want to authenticate. This is the project name you recorded in Google Analytics API Provider Setup. | string |
oauth.callback.url | The Callback URL for the application you registered with Google Analytics. This is https://auth.cloudelements.io/oauth , the Redirect URL that you added to your app during Google Analytics API Provider Setup. | string |
oauth.api.key | The key obtained from registering your app with the provider. This is the Client ID that you recorded in Google Analytics API Provider Setup. | string |
oauth.api.secret | The client secret obtained from registering your app with the provider. This is the Client Secret that you recorded in Google Analytics API Provider Setup. | string |
tags | Optional. User-defined tags to further identify the instance. | string |
Example Response for an Authenticated Element Instance
{
"id": 594927,
"name": "myCoolInstance",
"createdDate": "2019-07-12T15:29:52Z",
"token": "piohfasopeh9y3h1iofhpohr0a890t19r12r80",
"element": {
"id": 6830,
"name": "Google Analytics",
"key": "googleanalytics",
"description": "Analytics Hub to access Google Analytics",
"image": "https://images.cloudelements.io/google-analytics-logo-879A9F173A-seeklogo.com201805181553.png",
"logo": "googleanalytics",
"active": true,
"deleted": false,
"typeOauth": false,
"trialAccount": false,
"resources": [ ],
"objects": [ ],
"transformationsEnabled": true,
"bulkDownloadEnabled": true,
"bulkUploadEnabled": true,
"cloneable": true,
"extendable": true,
"beta": false,
"authentication": {
"type": "oauth2"
},
"extended": false,
"useModelsForMetadata": true,
"hub": "analytics",
"protocolType": "http",
"parameters": [ ],
"private": false
},
"user": {
"id": 184978
}
}