Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
foresight
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
libraries
foresight
Commits
370a70de
Commit
370a70de
authored
Aug 02, 2018
by
James Ooi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add README
parent
02efffc0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
144 additions
and
0 deletions
+144
-0
README.md
README.md
+144
-0
No files found.
README.md
0 → 100644
View file @
370a70de
# Foresight
### Foresight is an analytics library that allows for declarative event tracking in your websites.
*
Lightweight (<3kb minified and gzipped)
*
Declarative event tracking
*
Performant
## Installation
### Direct download
Download and install the latest distribution file from this
Git repository and load it in your webpage. For production cases, we encourage you to download the minified version (
[
`foresight.min.js`
](
#
)
)
```
html
<script
src=
"/path/to/foresight.min.js"
></script>
```
### Installation via `npm` or `yarn`
While this package is not available in the npm registry, you can still use package managers to install Foresight.
```
bash
$
npm install git+https://git.forefront.com.my/james/foresight.git
# or
$
yarn add git+https://git.forefront.com.my/james/foresight.git
```
```
js
// ES6 Import
import
Foresight
from
'foresight'
;
// CommonJS
var
Foresight
=
require
(
'foresight'
);
```
## Basic Usage
To initialise Foresight, call its constructor. Pass an optional configuration object as a parameter if needed.
```
js
var
foresight
=
new
Foresight
({
/* ... */
});
```
You can then track elements by adding HTML attributes on elements. The value of
the attribute configures the event to be sent, using the following format:
```
ini
data-track
=
"<event category>;<event action>;<event label>"
```
`event category`
and
`event action`
are required fields, while
`event label`
is optional.
---
### Tracking clicks
Clicks are tracked by setting the
`data-track`
attribute.
```
html
<a
href=
"/register"
data-track=
"home;register cta"
>
Register
</a>
```
The above markup would send the following event data when clicked:
| Field | Value |
| --------------- | -------------- |
|
`eventCategory`
|
`home`
|
|
`eventAction`
|
`register cta`
|
|
`eventLabel`
|
`null`
|
---
### Tracking views
Views are tracked by setting the
`data-track-view`
attribute.
```
html
<div
class=
"project"
data-track-view=
"home;view projects;project a"
>
<!-- Content -->
</div>
```
The above markup would send the following event data when the
`div`
enters the viewport:
| Field | Value |
| --------------- | --------------- |
|
`eventCategory`
|
`home`
|
|
`eventAction`
|
`view projects`
|
|
`eventLabel`
|
`project a`
|
---
## Configuration
```
ts
{
/** Defer tracking initialisation. */
defer
?:
boolean
/** Configure the intersection observer. */
observerOptions
?:
IntersectionObserverConfig
/** Treat clicks as an interactive event. Defaults to `true`. */
clicksAreInteractions
?:
boolean
/** Treat views as an interactive event. Defaults to `false`. */
viewsAreInteractions
?:
boolean
}
```
## Methods
#### `track(element: Element)`
Start tracking an element for events.
This method is useful if you dynamically add new DOM elements.
Parameters:
*
`element: Element`
The element to track.
---
#### `untrack(element: Element)`
Stop tracking an element for events.
Parameters:
*
`element: Element`
The element to track.
---
#### `start(root: Element)`
Start tracking all elements with tracking attributes for events. You don't need to call this method unless you set the
`defer`
option to
`true`
.
This method is idempotent, you can call it multiple times and it won't add duplicate listeners.
Parameters:
*
`root: Element`
The container to scan for elements. Defaults to
`body`
.
---
Copyright © 2018 FOREFRONT International Sdn Bhd.
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment