npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gacrawler

v1.1.6

Published

get some data from ga

Downloads

4

Readme

npm version license

gacrawler

Crawl some basic data from Google Analytics.

Installing

npm install gacrawler --save

Usage

Single request

First, register you environment variables

$ export GOOGLE_SERICES_EMAIL=*****@developer.gserviceaccount.com
$ export GA_VIEWID=my-bucket
$ export GCS_PRIVATE_KEY=`cat path/to/google-services-private-key.pem`

Then, crawl data using queryPageviewAndUniqueVisitors

var gaCrawler = require('gacrawler');
var crawler = new gaCrawler.gaCrawler();

var startdate = '2016-06-15';
var enddate = new Date();

crawler.queryPageviewAndUniqueVisitors('/', startdate, enddate).then(function(data){
	console.log(data); //output: { totalPageviews: '208873', totalUniqueUsers: '28157' }
}).catch(function(err){
	console.log(err);
});

If gaCrawler receives a RateLimited error from Google, it will retries until the correct data is returned.

Alternatively, you can set authentication manually using

var gaCrawler = require('gacrawler');

var VIEWID = 'ga:106358211';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yQBhs7mk9b6WTvmcz/B4KsRWt3puT2Ly4BFWrAPXtQAck/yCpjtOa\n\
99zqCjKMLq0gSsrk6Ixs1PAoGAWt59ATFOscqm/violIjx9y3+nsopGafVgVwAU1\n\
KUc7gSDA6YYC604oZdRz4azCdpp+8+SuTc3R39nFZHvui3ETlfSwGKhY4VecmcYK\n\
oPpFLcqExSQL7VUT4hrw2gQRx/2FXPTjFvOvY+CpjwCXFdVdAx3UPfMlz+dEjz0H\n\
nm1Z/bJsuEum4mz9wqWHhKUCgYEAtOL++1NmfyHvb0dAp5/xm3T5FOujhj4glsai\n\
GxMMR0kNx4sWxqKK46qRmPImbjC1Xg9kFDAxigUH+TsYokkVWPLqyzJejrqVc0Tp\n\
LQ/oQIR1izKtDuuNoGMn8zbJQhXrgj9pP05sSSUzdaW/p1P0cnq2NdsPFNqo5gIQ\n\
Z3ULy4ECgb/FLk5vIlwZ+8RxoVUkCeGRrpf7YEAgEl32/1lUKMDYNGnWg4j4oTCL\n\
vfjDa0D/JKd3RUQqgkMfm8oGk/u9uYxMkqqhbEMeJYaNlKBcTeyDLkbYr1w3TynB\n\
2+KhqQReUagY2HQixDJj4Pds99Jf5dIQAdpTvJo1BQR5fu5T/pScI6J=\n\
-----END RSA PRIVATE KEY-----';

var crawler = new new gaCrawler.gaCrawler(VIEWID, clientEmail, privateKey);

crawler.queryPageviewAndUniqueVisitors('/','2016-06-01', '2016-06-15').then(function(data){
	console.log(data); //output: { totalPageviews: '208873', totalUniqueUsers: '28157' }
}).catch(function(err){
	console.log(err);
});

gacrawler first will try to authorizate using clientEmail and privateKey, you can get these information by creating new credential at https://console.developers.google.com/apis/credentials

It also need your GA view id, to get it, login to Google Analytics, go to menu ADMIN, you will see the view setting on the third column, click to the menu and it will show you your view id, example

View ID
106358211

Note, you have to add 'ga:' prefix into view id before pass it in the queryPageviewAndUniqueVisitors, it will look like query.queryPageviewAndUniqueVisitors('ga:106358211',..)

Batch query

You can do batch query by calling batchQueryPageviewAndUniqueVisitors, parameters is the same as queryPageviewAndUniqueVisitors, except for the first parameter, it instead takes an arrays of urls.

crawler.batchQueryPageviewAndUniqueVisitors(['/','/2','/3'], '2016-06-01', '2016-06-15').then(function(data){
	console.log(data); 
	//output: [
	//{ url: '/', totalPageviews: '203', totalUniqueUsers: '287' },
	//{ url: '/2', totalPageviews: '2873', totalUniqueUsers: '257' },
	//{ url: '/3', totalPageviews: '2083', totalUniqueUsers: '287' }
	//]
});

Output alway is an array contains the output which order is exactly the same as the input urls.

If there is an error, the function will try to pass err in the array, so you don't have to add catch statement for batch query. For examples, an output with error will look like:

output: [
{ url: '/', totalPageviews: '203', totalUniqueUsers: '287' },
{ err: {message: "error"} },
{ url: '/3', totalPageviews: '2083', totalUniqueUsers: '287' }
]

Test

npm test

License

This software is licensed under the terms of the MIT license. See the LICENSE.md files.

Questions

If you have any questions, please feel free to send me an email at [email protected].`