Utility function to track browsing activity in a Chrome extension
MIT License
Utility function to track browsing activity in a Chrome extension.
This package exports a single function startTrackingActivity
which can be used to track a user's browsing activity.
The following example demonstates its usage:
function onSessionStart(session) {
console.log("START", session.startTime, session.url);
}
function onSessionEnd(session) {
console.log("END", session.endTime, session.url);
}
var stopTracking = startTrackingActivity(onSessionStart, onSessionEnd);
startTrackingActivity
accepts two arguments:
onSessionStart
: Invoked each time the user loads a new page in the foreground tab or navigates to an existing page. The argument session
contains the url
, startTime
and tabId
for the new page.
onSessionEnd
: Invoked each time the user loads a new page in the foreground tab, changes the tab or window, closes the foreground tab, or leaves the browser window. The argument session
contains the url
, startTime
, tabId
and endTime
for the page that was closed/changed.
The return value of startTrackingActivity
is another function, which can called to discontinue tracking when it is no longer required.
IMPORTANT NOTE: To use startTrackingAcvitity
, you need to declare the "tabs" permission in your extension's manifest file.
The easiest way to add the utility startTrackingActivity
is to copy-paste the code from tracking.js
into your background script/page. It's only 70 lines!
<script>
tagFirst download the the file into your extension directory using the following command:
curl -O https://raw.githubusercontent.com/aakashns/chrome-track-activity/master/tracking.js
Then include it in your background page using a script tag:
<head>
<script src="tracking.js"></script>
</head>
Check out the example for an example of this approach.
npm
Install the package from npm by running:
npm install chrome-track-activity --save
Then include it in your script using require
:
var startTrackingActivity = require('chrome-track-activity');
The example
directory contains and example extension which uses startTrackingActivity
to log the browsing activity of the user.
First, clone the repository:
git clone https://github.com/aakashns/chrome-track-activity.git
Then, follow the instructions on this page to load the extension into your browser.
Then, visit chrome://extensions and inspect the background page:
Open the 'Console' tab, leave the inspector window, and continue using the browser normally. You should see a log of the browsing activity as you enter and leave pages, like this:
The logic for the extension lives is background.js.