Analytics
/

Ownstats

Using the ownstats plugin


Integration with Ownstats for analytics

Click to expand

Installation

npm install analytics
npm install @analytics/ownstats

How to use

The @analytics/ownstats package works in the browser and server-side in Node.js. To use, install the package, include in your project and initialize the plugin with analytics.

Below is an example of how to use the browser plugin.

import Analytics from 'analytics'
import ownstatsPlugin from '@analytics/ownstats'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    ownstatsPlugin({
      endpoint: 'my.ownstats.cloud',
      useAutomation: true,
      debug: true
    })
  ]
})

/* Track a page view */
analytics.page()

After initializing analytics with the ownstatsPlugin plugin, data will be sent into Ownstats whenever or analytics.page are called.

See additional implementation examples for more details on using in your project.

Platforms Supported

The @analytics/ownstats package works in the browser and server-side in Node.js

Browser usage

The Ownstats client side browser plugin works with these analytic api methods:

Browser API

import Analytics from 'analytics'
import ownstatsPlugin from '@analytics/ownstats'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    ownstatsPlugin({
      endpoint: 'my.ownstats.cloud',
      useAutomation: true,
      debug: true
    })
  ]
})

Configuration options for browser

Optiondescription
endpoint
required - string
Your Ownstats endpoint
useAutomation
required - boolean
Automatically trigger pageviews upon route changes for single page applications
debug
required - boolena
Debug mode (necessary for localhost testing)

Server-side usage

The Ownstats server-side node.js plugin works with these analytic api methods:

Server-side API

import Analytics from 'analytics'
import ownstatsPlugin from '@analytics/ownstats'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    ownstatsPlugin({
      endpoint: 'my.ownstats.cloud'
    })
  ]
})

Configuration options for server-side

Optiondescription
endpoint
required - string
Your Ownstats endpoint

Additional examples

Below are additional implementation examples.

Server-side ES6
import Analytics from 'analytics'
import ownstatsPlugin from '@analytics/ownstats'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    ownstatsPlugin({
      endpoint: 'my.ownstats.cloud'
    })
    // ...other plugins
  ]
})

/* Track a page view */
analytics.page()
Server-side Node.js with common JS

If using node, you will want to import the .default

const analyticsLib = require('analytics').default
const ownstatsPlugin = require('@analytics/ownstats').default

const analytics = analyticsLib({
  app: 'my-app-name',
  plugins: [
    ownstatsPlugin({
      endpoint: 'my.ownstats.cloud'
    })
  ]
})

/* Track a page view */
analytics.page()
Using in HTML

Below is an example of importing via the unpkg CDN. Please note this will pull in the latest version of the package.

<!DOCTYPE html>
<html>
  <head>
    <title>Using @analytics/ownstats in HTML</title>
    <script src="https://unpkg.com/analytics/dist/analytics.min.js"></script>
    <script src="https://unpkg.com/@analytics/ownstats/dist/@analytics/ownstats.min.js"></script>
    <script type="text/javascript">
      /* Initialize analytics */
      var Analytics = _analytics.init({
        app: 'my-app-name',
        plugins: [
          analyticsOwnstats({
            endpoint: 'my.ownstats.cloud',
            useAutomation: true,
            debug: true
          })
        ]
      })

      /* Track a page view */
      analytics.page()
    </script>
  </head>
  <body>
    ....
  </body>
</html>
Using in HTML via ES Modules

Using @analytics/ownstats in ESM modules.

<!DOCTYPE html>
<html>
  <head>
    <title>Using @analytics/ownstats in HTML via ESModules</title>
    <script>
      // Polyfill process.
      // **Note**: Because `import`s are hoisted, we need a separate, prior <script> block.
      window.process = window.process || { env: { NODE_ENV: 'production' } }
    </script>
    <script type="module">
      import analytics from 'https://unpkg.com/analytics/lib/analytics.browser.es.js?module'
      import analyticsOwnstats from 'https://unpkg.com/@analytics/ownstats/lib/analytics-plugin-ownstats.browser.es.js?module'
      /* Initialize analytics */
      const Analytics = analytics({
        app: 'analytics-html-demo',
        debug: true,
        plugins: [
          analyticsOwnstats({
            endpoint: 'my.ownstats.cloud',
            useAutomation: true,
            debug: true
          })
          // ... add any other third party analytics plugins
        ]
      })

      /* Track a page view */
      analytics.page()
    </script>
  </head>
  <body>
    ....
  </body>
</html>

See the full list of analytics provider plugins in the main repo.