|
13 | 13 |
|
14 | 14 | 'use strict';
|
15 | 15 |
|
16 |
| -var google = require('../../lib/googleapis.js'); |
17 |
| -var analytics = google.analytics('v3'); |
18 |
| -var OAuth2Client = google.auth.OAuth2; |
19 |
| - |
20 |
| -// Client ID and client secret are available at |
21 |
| -// https://code.google.com/apis/console |
22 |
| -var CLIENT_ID = 'YOUR CLIENT ID HERE'; |
23 |
| -var CLIENT_SECRET = 'YOUR CLIENT SECRET HERE'; |
24 |
| -var REDIRECT_URL = 'YOUR REDIRECT URL HERE'; |
25 |
| - |
26 |
| -var oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL); |
27 |
| - |
28 |
| -oauth2Client.setCredentials({ |
29 |
| - access_token: 'ACCESS TOKEN HERE' |
30 |
| -}); |
| 16 | +const google = require('googleapis'); |
| 17 | +const analytics = google.analytics('v3'); |
| 18 | +const sampleClient = require('../sampleclient'); |
31 | 19 |
|
32 | 20 | // Custom Goals must be exist prior to used as an objectiveMetric
|
33 |
| -var objectiveMetric = 'ga:goal1Completions'; |
| 21 | +const objectiveMetric = 'ga:goal1Completions'; |
34 | 22 |
|
35 | 23 | // Serving frameworks listed below:
|
36 | 24 | // https://developers.google.com/analytics/devguides/platform/experiments#serving-framework
|
37 |
| -var servingFramework = 'API'; |
| 25 | +const servingFramework = 'API'; |
38 | 26 |
|
39 | 27 | // Invalid URLs are used when user is not redirected when showing an experiment
|
40 | 28 | // Read more: http://goo.gl/oVwKH1
|
41 |
| -var variations = [ |
| 29 | +const variations = [ |
42 | 30 | {'name': 'Default', 'url': 'http://www.example.com', 'status': 'ACTIVE'},
|
43 |
| - {'name': 'Variation 1', 'url': 'http://www.1.com', 'status': 'ACTIVE'}, |
44 |
| - {'name': 'Variation 2', 'url': 'http://www.2.com', 'status': 'ACTIVE'} |
| 31 | + {'name': 'variation 1', 'url': 'http://www.1.com', 'status': 'ACTIVE'}, |
| 32 | + {'name': 'variation 2', 'url': 'http://www.2.com', 'status': 'ACTIVE'} |
45 | 33 | ];
|
46 | 34 |
|
47 | 35 | // Specify Experiment configuration
|
48 |
| -var resourceBody = { |
| 36 | +const resourceBody = { |
49 | 37 | 'name': 'Example Experiment',
|
50 | 38 | 'status': 'READY_TO_RUN',
|
51 | 39 | 'objectiveMetric': objectiveMetric,
|
52 | 40 | 'servingFramework': servingFramework,
|
53 | 41 | 'variations': variations
|
54 | 42 | };
|
55 | 43 |
|
56 |
| -analytics.management.experiments.insert({ |
57 |
| - auth: oauth2Client, |
58 |
| - accountId: 'your-accountId', |
59 |
| - webPropertyId: 'your-webPropertyId', |
60 |
| - profileId: 'your-profileId', |
61 |
| - resource: resourceBody |
62 |
| -}, function (err, body) { |
| 44 | +const scopes = [ |
| 45 | + 'https://www.googleapis.com/auth/analytics' |
| 46 | +]; |
| 47 | + |
| 48 | +sampleClient.authenticate(scopes, (err, authClient) => { |
63 | 49 | if (err) {
|
64 |
| - return console.log(err); |
| 50 | + throw err; |
65 | 51 | }
|
66 |
| - console.log(body); |
| 52 | + analytics.management.experiments.insert({ |
| 53 | + auth: authClient, |
| 54 | + accountId: 'your-accountId', |
| 55 | + webPropertyId: 'your-webPropertyId', |
| 56 | + profileId: 'your-profileId', |
| 57 | + resource: resourceBody |
| 58 | + }, (err, body) => { |
| 59 | + if (err) { |
| 60 | + throw err; |
| 61 | + } |
| 62 | + console.log(body); |
| 63 | + }); |
67 | 64 | });
|
0 commit comments