From f6238f77f4afa5554ff69844bcac65b14c6feafa Mon Sep 17 00:00:00 2001 From: Rich Parrish Date: Tue, 10 Dec 2013 15:44:06 -0800 Subject: [PATCH] Example of a write operation using the Analytics Management API --- examples/analyticswrite.js | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 examples/analyticswrite.js diff --git a/examples/analyticswrite.js b/examples/analyticswrite.js new file mode 100644 index 0000000000..7722a8db00 --- /dev/null +++ b/examples/analyticswrite.js @@ -0,0 +1,73 @@ +/** + * Copyright 2013 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var googleapis = require('../lib/googleapis.js'); + +// Client ID and client secret are available at +// https://code.google.com/apis/console +var CLIENT_ID = 'YOUR CLIENT ID HERE'; +var CLIENT_SECRET = 'YOUR CLIENT SECRET HERE'; +var REDIRECT_URL = 'YOUR REDIRECT URL HERE'; + +var auth = new googleapis.OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); +auth.setCredentials({ + access_token: 'ACCESS TOKEN HERE' +}); + +// Custom Goals must be exist prior to used as an objectiveMetric +var objectiveMetric = 'ga:goal1Completions'; + +// Serving frameworks listed below: +// https://developers.google.com/analytics/devguides/platform/experiments#serving-framework +var servingFramework = 'API'; + +// Non-valid URLs are used when the user is not redirected when showing an experiment +// Read more: https://developers.google.com/analytics/devguides/platform/experiments#variations +var variations = [ + {'name': 'Default', 'url': 'http://www.example.com', 'status': 'ACTIVE'}, + {'name': 'Variation 1', 'url': 'http://www.1.com', 'status': 'ACTIVE'}, + {'name': 'Variation 2', 'url': 'http://www.2.com', 'status': 'ACTIVE'} +]; + +// Specify Experiment configuration +var resourceBody = { + 'name': 'Example Experiment', + 'status': 'READY_TO_RUN', + 'objectiveMetric': objectiveMetric, + 'servingFramework': servingFramework, + 'variations': variations +}; + +// Load Google Analytics v3 API resources and methods +googleapis + .discover('analytics', 'v3') + .execute(function(err, client) { + + // Insertion example + client + .analytics.management.experiments.insert({ + accountId: accountId, + webPropertyId: webPropertyId, + profileId: profileId + }, resourceBody) + .withAuthClient(auth) + .execute(function(err, result) { + if (err) { + console.log("Error", err); + return; + } + }); +});