|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// [START app] |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var express = require('express'); |
| 19 | +var request = require('request'); |
| 20 | + |
| 21 | +var app = express(); |
| 22 | + |
| 23 | +// The following environment variable is set by app.yaml when running on GAE, |
| 24 | +// but will need to be manually set when running locally. See README.md. |
| 25 | +var GA_TRACKING_ID = process.env.GA_TRACKING_ID; |
| 26 | + |
| 27 | +function trackEvent(category, action, label, value, cb) { |
| 28 | + var data = { |
| 29 | + v: '1', // API Version. |
| 30 | + tid: GA_TRACKING_ID, // Tracking ID / Property ID. |
| 31 | + // Anonymous Client Identifier. Ideally, this should be a UUID that |
| 32 | + // is associated with particular user, device, or browser instance. |
| 33 | + cid: '555', |
| 34 | + t: 'event', // Event hit type. |
| 35 | + ec: category, // Event category. |
| 36 | + ea: action, // Event action. |
| 37 | + el: label, // Event label. |
| 38 | + ev: value, // Event value. |
| 39 | + }; |
| 40 | + |
| 41 | + request.post( |
| 42 | + 'http://www.google-analytics.com/collect', { |
| 43 | + form: data |
| 44 | + }, |
| 45 | + function(err, response) { |
| 46 | + if (err) { return cb(err); } |
| 47 | + if (response.statusCode !== 200) { |
| 48 | + return cb(new Error('Tracking failed')); |
| 49 | + } |
| 50 | + cb(); |
| 51 | + } |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +app.get('/', function(req, res, next) { |
| 56 | + trackEvent( |
| 57 | + 'Example category', |
| 58 | + 'Example action', |
| 59 | + 'Example label', |
| 60 | + '100', // Event value must be numeric. |
| 61 | + function(err) { |
| 62 | + // This sample treats an event tracking error as a fatal error. Depending |
| 63 | + // on your application's needs, failing to track an event may not be |
| 64 | + // considered an error. |
| 65 | + if (err) { return next(err); } |
| 66 | + res.status(200).send('Event tracked.'); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +// Start the server |
| 71 | +var server = app.listen(process.env.PORT || '8080', '0.0.0.0', function() { |
| 72 | + console.log('App listening at http://%s:%s', server.address().address, |
| 73 | + server.address().port); |
| 74 | + console.log('Press Ctrl+C to quit.'); |
| 75 | +}); |
| 76 | +// [END app] |
0 commit comments