GithubHelp home page GithubHelp logo

isabella232 / websiteanalytics Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webiny/websiteanalytics

0.0 0.0 0.0 20 KB

Library that gathers common analytics data about your website visitors

License: MIT License

PHP 100.00%

websiteanalytics's Introduction

Website Analytics

This component helps you track your website visitors and some common attributes around them.

The component tracks 3 different metrics:

  • visitors => unique website visitors
  • page views => non-unique website visitors
  • url views => page view metrics for a particular url (not unique)

For visitors and url views additional attributes are tracked:

  • browser => which browsers did the visitors use, eg, chrome, firefox .. etc
  • country => from which country did the visitors come
  • device => which device did they use, mobile, tablet or desktop
  • os => which operating system they have
  • referrer domain => from which domain did they come to your website
  • referrer type => did they come from a search engine, social network, some other referral or is it a direct visit

Setup

Dependencies

The component requires an instance of Webiny/AnalyticsDb. Optionally, if you wish to track the country data, an instance of Webiny/GeoIp is required.

Tracking

To track the visitor data, you have to create the instance of WebsiteAnalytics and call the saveStats method, like so:

// get mongo instance for AnalyticsDb
$mongo = new \Webiny\Component\Mongo\Mongo('127.0.0.1:27017', 'MyDatabase');

// create AnalyticsDb instance
$a = new \Webiny\AnalyticsDb\AnalyticsDb($mongo);

// create WebsiteAnalytics instance
$wa = new \Webiny\WebsiteAnalytics\WebsiteAnalytics($a);

// save the stats
$wa->saveStats();

In order to track the country data, you need to also have the GeoIp instance:

// GeoIp provider
$geo = new \Webiny\GeoIp\GeoIp(new \Webiny\GeoIp\Provider\FreeGeoIp\FreeGeoIp());

$mongo = new \Webiny\Component\Mongo\Mongo('127.0.0.1:27017', 'MyDatabase');
$a = new \Webiny\AnalyticsDb\AnalyticsDb($mongo);


$wa = new \Webiny\WebsiteAnalytics\WebsiteAnalytics($a, $geo);
$wa->saveStats();

Query data

In order to query your data, you need to call the query method, which will return an instance of Query class. The query method requires 1 parameter and that is the time range within which the data should be queried. The date range is an array with 2 unix timestamps. Optionally you can use Webiny\AnalyticsDb\DateHelper class to get some commonly used date ranges, like lastMonth, thisWeek ... etc.

$wa = new \Webiny\WebsiteAnalytics\WebsiteAnalytics($a, $geoIp);
// query between two dates
$stats = $wa->query([1446336000, 1451606400]);

// optionally: use the DateHelper class
$stats = $wa->query(\Webiny\AnalyticsDb\DateHelper::rangeLast90Days());

Once you have the Query instance, which is returned by the query method, you can access the following methods to query your data:

visitorSum

Return the total number of unique visitors for the given period.

$stats->visitorsSum();

Returns a number with a total number of visitors.

10342

visitorsByDay

Returns the total number of visitors, per day for the given period in form of an array.

$stats->visitorsByDay();

The _id field represents the day timestamp, and the totalCount represents the number of visitors for that day.

Array
(
    [0] => Array
        (
            [_id] => 1443916800
            [totalCount] => 20
        )

    [1] => Array
        (
            [_id] => 1444003200
            [totalCount] => 23
        )

    [2] => Array
        (
            [_id] => 1444089600
            [totalCount] => 22
        )
    ...
)

visitorsByMonth

Returns the total number of visitors, per month for the given period in form of an array.

$stats->visitorsByMonth();

The _id field represents the day timestamp, and the totalCount represents the number of visitors for that day.

Array
(
    [0] => Array
        (
            [_id] => 1446336000
            [totalCount] => 797
        )

    [1] => Array
        (
            [_id] => 1448928000
            [totalCount] => 839
        )

    [2] => Array
        (
            [_id] => 1451606400
            [totalCount] => 42
        )

)

visitorsDimensionSum

Dimensions are attributes like browser, country, device, and other prior mentioned attributes on the top of the page.

The visitorsDimensionSum method returns a sum of a defined dimension value, of your visitors, for the given time period.

The method takes a dimension name, and optionally a dimension value. If you only provide a dimension name, the results will be grouped by different dimension values, like so:

$stats->visitorsDimensionSum(Webiny\WebsiteAnalytics\StatHandlers\Browser::NAME);

Result:

(
    [0] => Array
        (
            [_id] => Array
                (
                    [name] => browser
                    [value] => safari
                )

            [totalCount] => 1284
        )

    [1] => Array
        (
            [_id] => Array
                (
                    [name] => browser
                    [value] => chrome
                )

            [totalCount] => 468
        )
    ...
)

Additionally if you set a dimension value, the method will return a sum only for that given value:

$stats->visitorsDimensionSum(Webiny\WebsiteAnalytics\StatHandlers\Browser::NAME, 'safari');

Result:

1284

visitorsDimensionByDay

Returns a the number of visitors for the given dimension, grouped by day. Note that this method requires that you provide both dimension name and dimension value.

$stats->visitorsDimensionByDay(Webiny\WebsiteAnalytics\StatHandlers\Country::NAME, 'GB');

Result:

Array
(
    [0] => Array
        (
            [_id] => 1443916800
            [totalCount] => 3
        )

    [1] => Array
        (
            [_id] => 1444003200
            [totalCount] => 1
        )
    ...
)

pageViewsSum

Same as visitorSum method, this one just returns the sum of page views which are not unique visitors.

$stats->pageViewsSum();

pageViewsByDay

Same as visitorsByDay method, this one just returns the page views which are not unique visitors.

$stats->pageViewsByDay();

pageViewsByMonth

Same as visitorsByMonth method, this one just returns the page views which are not unique visitors.

$stats->pageViewsByMonth();

urlSum

Returns a sum of page views for a particular url. If you don't specify the url, you can get a list of top visited pages, for example:

// get top 10 pages
$stats->urlSum(null, 10);

The _id field represents the page url. NOTE: Pages are tracked without the query parameters.

Array
(

    [0] => Array
        (
            [_id] => /page-208/
            [totalCount] => 46
        )

    [1] => Array
        (
            [_id] => /page-396/
            [totalCount] => 45
        )
    ...
)

If you specify the page url, then the sum for that particular page is returned:

$stats->urlSum('/page-208/');
28

urlByDay

Same as visitorsByDay method, this one just returns the page views for a particular page, grouped by days.

$stats->urlByDay('/page-101/');

urlByMonth

Same as visitorsByMonth method, this one just returns the page views for a particular page, grouped by months.

$stats->urlByMonth('/page-101/');

urlDimensionSum

Same as visitorsDimensionSum method.

$result = $stats->urlDimensionSum('/page-110/', Webiny\WebsiteAnalytics\StatHandlers\Browser::NAME);

// or
$result = $stats->urlDimensionSum('/page-110/', Webiny\WebsiteAnalytics\StatHandlers\Browser::NAME, 'safari');

urlDimensionByDay

Same as visitorsDimensionByDay method, with the exception that you don't need to provide a dimension value.

$stats->urlDimensionByDay('/page-110/', Webiny\WebsiteAnalytics\StatHandlers\Browser::NAME);

// or

$stats->urlDimensionByDay('/page-110/', Webiny\WebsiteAnalytics\StatHandlers\Browser::NAME, 'safari');

Adding custom dimensions (attribute)

To add a custom dimension, create a class and implement StatHandlerInterface and then register your stat handler with the WebsiteAnalytics instance, like so:

$waInstance = new \Webiny\WebsiteAnalytics\WebsiteAnalytics($analyticDb, $geo);
$myHandlerInstance = $waInstance->addStatHandler('My\Custom\Handler\Name');

Now your handler is registered and will track the registered attribute on each url visit and on each unique visitor. You can also query your attribute analytics by using any of the provided *Dimension* methods, like urlDimensionByDay:

$stats->visitorsDimensionSum('my-attribute-name');

License and Contributions

Contributing > Feel free to send PRs.

License > MIT

Resources

To run unit tests, you need to use the following command:

$ cd path/to/WebsiteAnalytics/
$ composer install
$ phpunit

websiteanalytics's People

Contributors

svenalhamad avatar

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.