1.0.1 • Published 1 year ago

@pouch/extension-megatron v1.0.1

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

Megatron analytics library

Requires next permissions in manifest.json:

  • alarms - send keep-alive notifications
  • storage - store customerHash and last keep-alive notification date

Usage

Init Megatron singleton object anytime when all required params loaded/available:

import { megatron } from '@pouch/extension-megatron'

await megatron.init({
  apiUrl: '{API URL}',
  customerHash: { customerHash }, // customerHash as string or undefined for auto-generation
  trackId: '{track ID}', // ask BI team for unique trackId
  trackUrlPrefix: 'https://website', // automatic URL prefix for "/page/urls"
  version: chrome.runtime.getManifest().version,
});

Track page view (can be called even before .init()):

import { megatron } from '@pouch/extension-megatron'

// `trackUrlPrefix` will be added automatically before for all URLs started with "/"
await megatron.trackPageView({
  title: 'optional page title',
  url: '/page/url' 
});

Keep alive notifications:

import { megatron } from '@pouch/extension-megatron'

// Recommended check interval is 5-10 minutes, Megatron will send it only once per 24h
chrome.alarms.create('megatron:ping', { periodInMinutes: 10 });

// add global onAlarm listener or add logic to the beginning of existing listener
chrome.alarms.onAlarm.addListener((alarm) => {
  if(alarm.name === 'megatron:ping'){
    megatron.init({...});
    megatron.ping();
    return;
  }
  
  // ... other event handlers
});