Ejemplo n.º 1
0
	getAnalyticsProfiles(accessToken: string, callback: (e: Error, data?: any) => void) {
		var oauth2 = this.createOAuth2();
		oauth2.setCredentials({
			access_token: accessToken
		});
		googleapis.options({
			auth: oauth2
		});
		var analytics = googleapis.analytics({ version: 'v3' });
		var options = {
			accountId: '~all',
			webPropertyId: '~all'
		};
		analytics.management.profiles.list(options, (e: Error, result?: any) => {
			if (e) {
				callback(e);
				return;
			}
			callback(null, result);
		});
	}
Ejemplo n.º 2
0
	getAnalyticsData(
		accessToken: string,
		profileId: string,
		startDate: Date,
		endDate: Date,
		metrics: string[],
		dimensions: string[],
		sort: string,
		filters: string[],
		segment: string,
		callback: (e: Error, data?: any) => void
	) {
		var limitOnPage = 10000;
		var oauth2 = this.createOAuth2();
		oauth2.setCredentials({
			access_token: accessToken
		});
		googleapis.options({
			auth: oauth2
		});
		var analytics = googleapis.analytics({ version: 'v3' });
		var options: any = {
			'ids': profileId,
			'start-date': moment(startDate).format('YYYY-MM-DD'),
			'end-date': moment(endDate).format('YYYY-MM-DD'),
			'metrics': metrics ? metrics.join(',') : '',
			'max-results': limitOnPage
		};
		if (dimensions) {
			options.dimensions = dimensions.join(',');
		}
		if (sort) {
			options.sort = sort;
		}
		if (filters) {
			options.filters = filters.join(',');
		}
		if (segment) {
			options.segment = segment;
		}

		var allResult: any = null;
		var noMoreResults = false;
		var iterations = _.range(0, 200);
		each(iterations)
		.on('item', (i: number, next: (e?: Error) => void) => {
			if (noMoreResults) {
				return next();
			}
			options['start-index'] = i * limitOnPage + 1;
			analytics.data.ga.get(options, (e: Error, result?: any) => {
				if (e) return next(e);
				if (!result.rows || result.rows.length == 0) {
					noMoreResults = true;
				} else {
					if (allResult === null) {
						allResult = result;
					} else {
						allResult.rows.splice.apply(allResult.rows, [allResult.rows.length, 0].concat(result.rows));
					}
				}
				next(null);
			});
		})
		.on('error', (e: Error) => callback(e))
		.on('end', () => callback(null, allResult))
		.parallel(1);
	}