Git Product home page Git Product logo

pageviews.js's Introduction

pageviews.js

Greenkeeper badge

A lightweight JavaScript client library for the Wikimedia Pageviews API for Wikipedia and various of its sister projects for Node.js and the browser.

Installation

With npm:

$ npm install pageviews

Usage in Node.js

The client library requires native or polyfilled Promises support. Below are some samples of how to use it in practice.

var pageviews = require('pageviews');

// Getting pageviews for a single article
pageviews.getPerArticlePageviews({
  article: 'Berlin',
  project: 'en.wikipedia',
  start: new Date(new Date() - 3 * 24 * 60 * 60 * 1000), // YYYYMMDD string or Date object
  end: new Date(new Date() - 2 * 24 * 60 * 60 * 1000) // YYYYMMDD string or Date object
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting pageviews for multiple articles
pageviews.getPerArticlePageviews({
  articles: ['Berlin', 'Hamburg'], // Plural
  project: 'en.wikipedia',
  start: new Date(new Date() - 3 * 24 * 60 * 60 * 1000),  // YYYYMMDD string or Date object
  end: new Date(new Date() - 2 * 24 * 60 * 60 * 1000) // YYYYMMDD string or Date object
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting aggregated pageviews for a single project
pageviews.getAggregatedPageviews({
  project: 'en.wikipedia',
  start: '2015120101', // YYYYMMDDHH string or Date object
  end: '2015120102' // YYYYMMDDHH string or Date object
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting aggregated pageviews for multiple projects
pageviews.getAggregatedPageviews({
  projects: ['en.wikipedia', 'de.wikipedia'], // Plural
  start: '2015120101', // YYYYMMDDHH string or Date object
  end: '2015120101' // YYYYMMDDHH string or Date object
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting top-n items ranked by pageviews for a single project
pageviews.getTopPageviews({
  project: 'en.wikipedia',
  year: '2015',
  month: '12',
  day: '01',
  limit: 2 // Limit to the first n results
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting top-n items ranked by pageviews for multiple projects
pageviews.getTopPageviews({
  projects: ['en.wikipedia', 'de.wikipedia'], // Plural
  year: '2015',
  month: '12', // Can also use integers like 12
  day: '01', // Can also use integers like 1
  limit: 2 // Limit to the first n results
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting top-n items ranked by pageviews for multiple projects
pageviews.getTopPageviews({
  projects: ['en.wikipedia', 'de.wikipedia'], // Plural
  date: new Date(new Date() - 2 * 24 * 60 * 60 * 1000), // YYYYMMDD string or Date object
  limit: 2 // Limit to the first n results
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting unique devices
pageviews.getUniqueDevices({
  project: 'en.wikipedia',
  start: '20160301',
  end: '20160301',
  accessSite: 'desktop-site'
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

// Getting legacy pagecounts
pageviews.getAggregatedLegacyPagecounts({
  project: 'en.wikipedia',
  start: new Date(2008, 12, 1, 1),
  end: new Date(2008, 12, 1, 2)
}).then(function(result) {
  console.log(JSON.stringify(result, null, 2));
}).catch(function(error) {
  console.log(error);
});

Usage in the browser

You can build a minified version of pageviews.js by running the build script.

$ npm run build

You can then use the file in the browser as follows.

<script src="pageviews.min.js"></script>
<script>
  // Getting pageviews for a single article
  pageviews.getPerArticlePageviews({
    article: 'Berlin',
    project: 'en.wikipedia',
    start: new Date(new Date() - 3 * 24 * 60 * 60 * 1000), // YYYYMMDD string or Date object
    end: new Date(new Date() - 2 * 24 * 60 * 60 * 1000) // YYYYMMDD string or Date object
  }).then(function(result) {
    console.log(JSON.stringify(result, null, 2));
  }).catch(function(error) {
    console.log(error);
  });

  /* All functions as defined in the Node.js section */
</script>

API

The API is modeled along the Wikimedia Pageviews API and the Wikimedia Unique Devices API and offers the following methods:

/**
 * This is the root of all pageview data endpoints. The list of paths that
 * this returns includes ways to query by article, project, top articles,
 * etc. If browsing the interactive documentation, see the specifics for
 * each endpoint below.
 */
getPageviewsDimensions

/**
 * Given a Mediawiki article and a date range, returns a daily timeseries of
 * its pageview counts. You can also filter by access method and/or agent
 * type.
 */
getPerArticlePageviews

/**
 * Given a date range, returns a timeseries of pageview counts. You can
 * filter by project, access method and/or agent type. You can choose
 * between daily and hourly granularity as well.
 */
getAggregatedPageviews

/**
 * Lists the 1000 most viewed articles for a given project and timespan
 * (year, month or day). You can filter by access method.
 */
getTopPageviews

/**
 * Given a date range between December 2007 and August 2016,
 * returns a timeseries of pageview counts. You can filter by
 * project and access method. You can choose between daily,
 * hourly and monthly granularity as well.
 */
 getAggregatedLegacyPagecounts

/**
 * Given a project and a date range, returns a timeseries of unique devices
 * counts. You can filter by access site and choose between daily and
 * monthly granularity.
 */
getUniqueDevices

Contributors

License

Copyright 2017 Thomas Steiner (@tomayac)

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.

NPM

pageviews.js's People

Contributors

addshore avatar greenkeeper[bot] avatar marcelrf avatar nuria avatar tomayac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pageviews.js's Issues

Code hangs in the main npm script

Hello,

Firstly, thanks for this well code wikimedia client !

Just to inform that there is a code at the bottom of the script that console.log each time we required it :
pageviews.getTopPageviewsByCountry({ project: 'fr.wikipedia', year: 2017, month: 12, access: 'mobile-web' }).then(function(result) { console.log(JSON.stringify(result, null, 2)) });

Have a nice day

January 2016 doesn't seem to be working

Not sure if intentional but confusing:

pageviews.getTopPageviews({
        project: 'en.wikipedia',
        year: 2016,
        month: 1,
        day: 1,
        limit: 15
    }).catch(function(error) {
      console.log(error);
    });
} );

Returns error [Error: Required parameter "month" missing or invalid.]
However
using month: "01" doesn't.
Using a number over 9 for day and month parameters however does work.
I would suggest testing for typeof Number and erroring out OR allowing numbers for these fields.

Problems when transforming Date object to string

Hi Thomas,
Thank you very much for creating this awesome lib!

I think I found two problems in the code that transforms a Date object into a string. I filed just one issue for both, because they are caused by the same couple lines of code. If they make sense, please let me know if you want me to create a pull request for that. Thanks :)

1. Passing a Date object belonging to October, November or December fails validation.
When building a date string from a Date object:

var pad = function(d) {
    return d < 10 ? '0' + d : d;
};

...

params.start = typeof params.start === 'object' ?
    (params.start.getFullYear() + (pad(params.start.getMonth() + 1)) +
        pad(params.start.getDate())) :
    params.start;

getFullYear, getMonth and getDate return numeric values. And pad only returns a string when actually padding. If the month is October, November or December, pad will return the value as is (numeric), and the concatenation will become a numerical sum, resulting in a validation error in the next regular expression check.

2. Passing a Date object with time zone information may result in shifted dates.
If the machine executing pageview.js has a timezone like GMT-5 and it tries to execute:

var pageviews = require('pageviews');

pageviews.getAggregatedPageviews({
    project: 'all-projects',
    granularity: 'daily',
    start: new Date('2016-01-01'),
    end: new Date('2016-01-31')
});

new Date('2016-01-01') will create a Date object with the following value Thu Dec 31 2015 19:00:00 GMT-0500 (EST). And the code that generates a date string uses getFullYear, getMonth and getDate, which in this particular case will return 2015, 12 and 31 respectively, generating a date string like 2015-12-31 instead of 2016-01-01. This would happen with the end date as well.

Cheers!

Does not seem to be working on Firefox at the moment

Hello,
I have tried your JS library following some issues I had with some legacy script of mine that was doing kind of the same thing and I notice the same issue with your library than with my code, so maybe you'll be better than me at finding a solution to fix this :)

The thing is, using a very basic HTML page containing the example from the README ( https://gist.github.com/symac/c98233f11f4a3006a50f4728bda5287a ), I have a page that works as expected using Chrome or Opera but not on Firefox. When working correctly I get the pageviews statistics, but the console of Firefox just displays :
{ "isTrusted": true }
and no pageviews data :(

I have not been able to find any error message so if any of you has an idea on how to fix this, I would be very happy to read you! I am using FF 53.0 64 bits on Linux.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.