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
41505fa1
Commit
41505fa1
authored
Aug 02, 2018
by
James Ooi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Build latest production builds
parent
48b6bcaa
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
161 additions
and
7 deletions
+161
-7
foresight.js
dist/foresight.js
+81
-5
foresight.min.js
dist/foresight.min.js
+0
-0
foresight.d.ts
typings/foresight.d.ts
+77
-2
toArray.d.ts
typings/utils/toArray.d.ts
+3
-0
No files found.
dist/foresight.js
View file @
41505fa1
...
@@ -100,6 +100,13 @@ return /******/ (function(modules) { // webpackBootstrap
...
@@ -100,6 +100,13 @@ return /******/ (function(modules) { // webpackBootstrap
"use strict"
;
"use strict"
;
/*!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
* @license MIT
* @copyright 2018 (c) FOREFRONT International Sdn Bhd
*/
var
__assign
=
(
this
&&
this
.
__assign
)
||
function
()
{
var
__assign
=
(
this
&&
this
.
__assign
)
||
function
()
{
__assign
=
Object
.
assign
||
function
(
t
)
{
__assign
=
Object
.
assign
||
function
(
t
)
{
for
(
var
s
,
i
=
1
,
n
=
arguments
.
length
;
i
<
n
;
i
++
)
{
for
(
var
s
,
i
=
1
,
n
=
arguments
.
length
;
i
<
n
;
i
++
)
{
...
@@ -113,12 +120,30 @@ var __assign = (this && this.__assign) || function () {
...
@@ -113,12 +120,30 @@ var __assign = (this && this.__assign) || function () {
};
};
__webpack_require__
(
1
);
__webpack_require__
(
1
);
var
Utils
=
__webpack_require__
(
2
);
var
Utils
=
__webpack_require__
(
2
);
var
Foresight
=
(
function
()
{
/**
* Foresight is an analytics library that allows for declarative event tracking
* in your websites.
* @class
*/
var
Foresight
=
/** @class */
(
function
()
{
/**
* @constructor
*/
function
Foresight
(
config
)
{
function
Foresight
(
config
)
{
var
_this
=
this
;
var
_this
=
this
;
/**
* Stores a mapping of elements with is respective functions to de-register
* listeners.
* @private
*/
this
.
_untrackFns
=
new
Map
();
this
.
_untrackFns
=
new
Map
();
/**
* Stores an instance of an IntersectionObserver.
* @private
*/
this
.
_observer
=
null
;
this
.
_observer
=
null
;
this
.
options
=
__assign
({},
Foresight
.
defaultOptions
,
config
);
this
.
options
=
__assign
({},
Foresight
.
defaultOptions
,
config
);
// Initialise IntersectionObserver
this
.
_observer
=
new
IntersectionObserver
(
function
(
entries
,
observer
)
{
this
.
_observer
=
new
IntersectionObserver
(
function
(
entries
,
observer
)
{
entries
.
forEach
(
function
(
entry
)
{
entries
.
forEach
(
function
(
entry
)
{
if
(
entry
.
isIntersecting
)
{
if
(
entry
.
isIntersecting
)
{
...
@@ -127,7 +152,14 @@ var Foresight = (function () {
...
@@ -127,7 +152,14 @@ var Foresight = (function () {
}
}
});
});
},
this
.
options
.
observerOptions
);
},
this
.
options
.
observerOptions
);
// Start tracking
if
(
!
this
.
options
.
defer
)
{
this
.
start
();
}
}
}
/**
* Start event tracking for all DOM elements with event tracking attributes.
*/
Foresight
.
prototype
.
start
=
function
(
root
)
{
Foresight
.
prototype
.
start
=
function
(
root
)
{
var
_this
=
this
;
var
_this
=
this
;
if
(
root
===
void
0
)
{
root
=
document
.
body
;
}
if
(
root
===
void
0
)
{
root
=
document
.
body
;
}
...
@@ -138,16 +170,21 @@ var Foresight = (function () {
...
@@ -138,16 +170,21 @@ var Foresight = (function () {
.
toArray
(
root
.
querySelectorAll
(
'[data-track], [data-track-view]'
))
.
toArray
(
root
.
querySelectorAll
(
'[data-track], [data-track-view]'
))
.
map
(
function
(
element
)
{
return
_this
.
track
(
element
);
});
.
map
(
function
(
element
)
{
return
_this
.
track
(
element
);
});
};
};
/**
* Enables event tracking for a DOM element with event tracking attribute.
*/
Foresight
.
prototype
.
track
=
function
(
element
)
{
Foresight
.
prototype
.
track
=
function
(
element
)
{
if
(
!
this
.
_untrackFns
.
has
(
element
))
{
if
(
!
this
.
_untrackFns
.
has
(
element
))
{
this
.
_untrackFns
.
set
(
element
,
{
click
:
null
,
view
:
null
});
this
.
_untrackFns
.
set
(
element
,
{
click
:
null
,
view
:
null
});
}
}
var
untrackFn
=
this
.
_untrackFns
.
get
(
element
);
var
untrackFn
=
this
.
_untrackFns
.
get
(
element
);
// Track clicks
if
(
element
.
getAttribute
(
'data-track'
)
!==
null
)
{
if
(
element
.
getAttribute
(
'data-track'
)
!==
null
)
{
if
(
untrackFn
.
click
==
null
)
{
if
(
untrackFn
.
click
==
null
)
{
untrackFn
.
click
=
this
.
_trackClicks
(
element
);
untrackFn
.
click
=
this
.
_trackClicks
(
element
);
}
}
}
}
// Track views
if
(
element
.
getAttribute
(
'data-track-view'
)
!==
null
)
{
if
(
element
.
getAttribute
(
'data-track-view'
)
!==
null
)
{
if
(
untrackFn
.
view
==
null
)
{
if
(
untrackFn
.
view
==
null
)
{
untrackFn
.
view
=
this
.
_trackViews
(
element
);
untrackFn
.
view
=
this
.
_trackViews
(
element
);
...
@@ -155,6 +192,9 @@ var Foresight = (function () {
...
@@ -155,6 +192,9 @@ var Foresight = (function () {
}
}
this
.
_untrackFns
.
set
(
element
,
untrackFn
);
this
.
_untrackFns
.
set
(
element
,
untrackFn
);
};
};
/**
* Disable event tracking for a DOM element.
*/
Foresight
.
prototype
.
untrack
=
function
(
element
)
{
Foresight
.
prototype
.
untrack
=
function
(
element
)
{
var
untrackFn
=
this
.
_untrackFns
.
get
(
element
);
var
untrackFn
=
this
.
_untrackFns
.
get
(
element
);
if
(
untrackFn
===
undefined
)
{
if
(
untrackFn
===
undefined
)
{
...
@@ -168,17 +208,30 @@ var Foresight = (function () {
...
@@ -168,17 +208,30 @@ var Foresight = (function () {
}
}
this
.
_untrackFns
.
delete
(
element
);
this
.
_untrackFns
.
delete
(
element
);
};
};
/**
* Parse an event string and returns a `EventData` object.
* @private
*/
Foresight
.
prototype
.
_parseEventString
=
function
(
eventString
)
{
Foresight
.
prototype
.
_parseEventString
=
function
(
eventString
)
{
var
split
=
eventString
.
split
(
';'
);
var
split
=
eventString
.
split
(
';'
);
var
category
=
split
[
0
],
action
=
split
[
1
],
label
=
split
[
2
];
var
category
=
split
[
0
],
action
=
split
[
1
],
label
=
split
[
2
];
// If only one argument is provided, then the argument is the action
if
(
split
.
length
===
1
)
{
if
(
split
.
length
===
1
)
{
action
=
category
;
action
=
category
;
category
=
undefined
;
category
=
undefined
;
}
}
return
{
category
:
category
,
action
:
action
,
label
:
label
,
interaction
:
true
};
return
{
category
:
category
,
action
:
action
,
label
:
label
,
interaction
:
true
};
};
};
/**
* Registers click listeners that triggers an analytics event when the element
* is clicked or middle clicked.
*
* @returns Returns a function to remove the event listener.
* @private
*/
Foresight
.
prototype
.
_trackClicks
=
function
(
element
)
{
Foresight
.
prototype
.
_trackClicks
=
function
(
element
)
{
var
_this
=
this
;
var
_this
=
this
;
// Define listen fucntion
var
listener
=
function
(
e
)
{
var
listener
=
function
(
e
)
{
_this
.
_onTrackedClick
(
element
,
e
);
_this
.
_onTrackedClick
(
element
,
e
);
};
};
...
@@ -189,6 +242,13 @@ var Foresight = (function () {
...
@@ -189,6 +242,13 @@ var Foresight = (function () {
element
.
removeEventListener
(
'auxclick'
,
listener
);
element
.
removeEventListener
(
'auxclick'
,
listener
);
};
};
};
};
/**
* Registers a view observer that triggers an analytics event when the element
* is in view.
*
* @returns Returns a function that disconnects the view observer.
* @private
*/
Foresight
.
prototype
.
_trackViews
=
function
(
element
)
{
Foresight
.
prototype
.
_trackViews
=
function
(
element
)
{
var
_this
=
this
;
var
_this
=
this
;
this
.
_observer
.
observe
(
element
);
this
.
_observer
.
observe
(
element
);
...
@@ -196,10 +256,14 @@ var Foresight = (function () {
...
@@ -196,10 +256,14 @@ var Foresight = (function () {
_this
.
_observer
.
unobserve
(
element
);
_this
.
_observer
.
unobserve
(
element
);
};
};
};
};
/**
* Handles a click event on an element that is being tracked by Foresight.
* @private
*/
Foresight
.
prototype
.
_onTrackedClick
=
function
(
element
,
event
)
{
Foresight
.
prototype
.
_onTrackedClick
=
function
(
element
,
event
)
{
var
s
=
element
.
getAttribute
(
'data-track'
);
var
s
=
element
.
getAttribute
(
'data-track'
);
var
data
=
this
.
_parseEventString
(
s
);
var
data
=
this
.
_parseEventString
(
s
);
data
.
interaction
=
!
this
.
options
.
nonInteractionClick
s
;
data
.
interaction
=
this
.
options
.
clicksAreInteraction
s
;
if
(
element
.
getAttribute
(
'data-track:non-interaction'
)
!==
null
)
{
if
(
element
.
getAttribute
(
'data-track:non-interaction'
)
!==
null
)
{
data
.
interaction
=
false
;
data
.
interaction
=
false
;
}
}
...
@@ -209,10 +273,14 @@ var Foresight = (function () {
...
@@ -209,10 +273,14 @@ var Foresight = (function () {
'non_interaction'
:
!
data
.
interaction
,
'non_interaction'
:
!
data
.
interaction
,
});
});
};
};
/**
* Handles a view event on an element that is being tracked by Foresight.
* @private
*/
Foresight
.
prototype
.
_onTrackedView
=
function
(
element
,
observer
)
{
Foresight
.
prototype
.
_onTrackedView
=
function
(
element
,
observer
)
{
var
s
=
element
.
getAttribute
(
'data-track-view'
);
var
s
=
element
.
getAttribute
(
'data-track-view'
);
var
data
=
this
.
_parseEventString
(
s
);
var
data
=
this
.
_parseEventString
(
s
);
data
.
interaction
=
!
this
.
options
.
nonInteractionView
s
;
data
.
interaction
=
this
.
options
.
viewsAreInteraction
s
;
if
(
element
.
getAttribute
(
'data-track-view:interaction'
)
!==
null
)
{
if
(
element
.
getAttribute
(
'data-track-view:interaction'
)
!==
null
)
{
data
.
interaction
=
true
;
data
.
interaction
=
true
;
}
}
...
@@ -222,9 +290,14 @@ var Foresight = (function () {
...
@@ -222,9 +290,14 @@ var Foresight = (function () {
'non_interaction'
:
!
data
.
interaction
,
'non_interaction'
:
!
data
.
interaction
,
});
});
};
};
/**
* Default Options
* @static
*/
Foresight
.
defaultOptions
=
{
Foresight
.
defaultOptions
=
{
nonInteractionClicks
:
false
,
defer
:
false
,
nonInteractionViews
:
true
,
clicksAreInteractions
:
true
,
viewsAreInteractions
:
false
,
observerOptions
:
{},
observerOptions
:
{},
};
};
return
Foresight
;
return
Foresight
;
...
@@ -980,6 +1053,9 @@ exports.toArray = toArray_1.default;
...
@@ -980,6 +1053,9 @@ exports.toArray = toArray_1.default;
"use strict"
;
"use strict"
;
Object
.
defineProperty
(
exports
,
"__esModule"
,
{
value
:
true
});
Object
.
defineProperty
(
exports
,
"__esModule"
,
{
value
:
true
});
/**
* Creates a new `Array` instance from an array-like or iterable object.
*/
function
toArray
(
arrayLike
)
{
function
toArray
(
arrayLike
)
{
return
[].
slice
.
call
(
arrayLike
);
return
[].
slice
.
call
(
arrayLike
);
}
}
...
...
dist/foresight.min.js
View file @
41505fa1
This diff is collapsed.
Click to expand it.
typings/foresight.d.ts
View file @
41505fa1
/*!
* Foresight
*
* @author James Ooi <james.ooi@forefront.com.my>
* @license MIT
* @copyright 2018 (c) FOREFRONT International Sdn Bhd
*/
import
'intersection-observer'
;
import
'intersection-observer'
;
/**
* Available options for configuring Foresight.
*/
interface
ForesightConfig
{
interface
ForesightConfig
{
/** Defer tracking initialisation. */
defer
?:
boolean
;
/** Configure the intersection observer. */
observerOptions
?:
IntersectionObserverInit
;
observerOptions
?:
IntersectionObserverInit
;
nonInteractionClicks
?:
boolean
;
/** Treat clicks as an interactive event. Defaults to true. */
nonInteractionViews
?:
boolean
;
clicksAreInteractions
?:
boolean
;
/** Treat views as an interactive event. Defaults to false. */
viewsAreInteractions
?:
boolean
;
}
}
/**
* Foresight is an analytics library that allows for declarative event tracking
* in your websites.
* @class
*/
declare
class
Foresight
{
declare
class
Foresight
{
/**
* Default Options
* @static
*/
static
defaultOptions
:
Partial
<
ForesightConfig
>
;
static
defaultOptions
:
Partial
<
ForesightConfig
>
;
/**
* Stores the options of the current Foresight instance.
* @public
*/
options
:
ForesightConfig
;
options
:
ForesightConfig
;
/**
* Stores a mapping of elements with is respective functions to de-register
* listeners.
* @private
*/
private
_untrackFns
;
private
_untrackFns
;
/**
* Stores an instance of an IntersectionObserver.
* @private
*/
private
_observer
;
private
_observer
;
/**
* @constructor
*/
constructor
(
config
:
ForesightConfig
);
constructor
(
config
:
ForesightConfig
);
/**
* Start event tracking for all DOM elements with event tracking attributes.
*/
start
(
root
?:
Element
):
void
;
start
(
root
?:
Element
):
void
;
/**
* Enables event tracking for a DOM element with event tracking attribute.
*/
track
(
element
:
Element
):
void
;
track
(
element
:
Element
):
void
;
/**
* Disable event tracking for a DOM element.
*/
untrack
(
element
:
Element
):
void
;
untrack
(
element
:
Element
):
void
;
/**
* Parse an event string and returns a `EventData` object.
* @private
*/
private
_parseEventString
;
private
_parseEventString
;
/**
* Registers click listeners that triggers an analytics event when the element
* is clicked or middle clicked.
*
* @returns Returns a function to remove the event listener.
* @private
*/
private
_trackClicks
;
private
_trackClicks
;
/**
* Registers a view observer that triggers an analytics event when the element
* is in view.
*
* @returns Returns a function that disconnects the view observer.
* @private
*/
private
_trackViews
;
private
_trackViews
;
/**
* Handles a click event on an element that is being tracked by Foresight.
* @private
*/
private
_onTrackedClick
;
private
_onTrackedClick
;
/**
* Handles a view event on an element that is being tracked by Foresight.
* @private
*/
private
_onTrackedView
;
private
_onTrackedView
;
}
}
export
=
Foresight
;
export
=
Foresight
;
typings/utils/toArray.d.ts
View file @
41505fa1
/**
* Creates a new `Array` instance from an array-like or iterable object.
*/
export
default
function
toArray
<
T
>
(
arrayLike
:
any
):
T
[];
export
default
function
toArray
<
T
>
(
arrayLike
:
any
):
T
[];
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