You can just install from npm or yarn, typescript definitions are included in the base package:
npm install insights-js
Then import as a commonjs
module:
const { init, track, parameters } = require("insights-js")
Or as an es6
module:
import { init, track, parameters } from "insights-js"
And track something:
// Initialize for your project, you can find this on your dashboard
init("XkJsfa5KasGf9W_8")
// ...
// track something
track({
id: "user-subscribed",
parameters: {
plan: "Startup"
}
})
<script src="https://getinsights.io/js/insights.js"></script>
<script>
// Initialize for your project,
// you can find this on your dashboard
insights.init("XkJsfa5KasGf9W_8")
// ...
// track page views
insights.trackPages()
// track some other event
insights.track({
id: "user-subcriptions",
parameters: {
plan: "Startup"
}
})
</script>
Just call track()
(or insights.track()
if you are using the UMD build):
// Initialize for your project,
// you can find this on your dashboard
init("XkJsfa5KasGf9W_8")
// ...
track({
id: "user-registered",
parameters: {
method: "google",
from: "front-page-link"
}
})
In addition to customer parameters, insights-js
ships with handy built-in parameters, to track values that are often useful, use as follow:
import { parameters } from "insights-js"
track({
id: "read-post",
parameters: {
// this will track the locale of the user, useful to know if we should translate our posts
locale: parameters.locale(),
// this will track the type of screen on which the user reads the post, useful for useability
screenSize: parameters.screenType()
}
})
See the full list in the API documentation.
Certain events last through time and may be undone or cancelled after they have been logged. For example, when tracking subscription to services or people.
For these events, it is very useful to be able to know:
tracked - cancelled
) eventsWhen this flag is set to true
, the given event is marked as cancelled:
// A user just subscribed!
track({
id: "user-subscribed",
parameters: {
plan: "Startup",
currency: "usd"
}
})
// A user unsubscribed.
track({
id: "user-subscribed",
parameters: {
plan: "Startup",
currency: "usd"
},
remove: true
})
insights-js
provides an automated way of collecting page views, just call thetrackPages()
function:
trackPages()
This function tracks the current page then listen for URL changes and track any subsequent page views. You may configure its behaviour by passing an options object.
The calls to init()
and track()
are wrappers are methods on the App
class. You may instantiate any use one app per project - with or without the default App:
import { App } from "insights-js"
// equivalent to init("project-1-id")
const app1 = new App("project-1-id")
const app2 = new App("project-2-id")
// will show up in project 1's dashboard
app1.track({
id: "user-registered",
parameters: {
method: "google",
from: "top-link"
}
})
// will show up in project 2's dashboard
app2.track({
id: "read-post",
parameters: {
// this will track the locale of the user, useful to know if we should translate our posts
locale: parameters.locale(),
// this will track the type of screen on which the user reads the post, useful for useability
screenSize: parameters.screenType()
}
})
init(projectId, options)
init(projectId: string, options?: InitOptions): void
projectId: string
Mandatory
options?: AppOptions
Optional
Default value: {}
options.ignoreErrors: boolean
Optional
Default value: false
true
any error that may occur when tracking events will be ignored. It is reccomended to set this flag to true
on production.options.disabled: boolean
Optional
Default value: false
true
, all calls are disabled. This flag is useful to disable the tracking based on the environment or URL.track(event)
track(event: Event): Promise<void>
event: Event
Mandatory
event.id: string
Mandatory
kebab-case
.event.parameters: { [key: string]: string }
Optional
Default value: {}
(key: string) -> (value: string)
pairs. Insights keeps track of the number of events logged for each value. You may also use the parameters
variable to generate built-in values. See the full list in the parameters'API documentation.event.unique: boolean
Optional
Default value: false
event.update: boolean
Optional
Default value: false
event.remove: boolean
Optional
Default value: false
Certain events last through time and may be undone or cancelled after they have been logged. For example, when tracking subscription to services or people.
For these events, it is very useful to be able to know:
tracked - cancelled
) events.When this flag is set to true
, the given event is marked as cancelled.
Examples:
import { track, parameters } from "insights-js"
// user signed up with their email/password
track({
id: "user-signed-up",
parameters: {
provider: "email"
}
})
// user signed up with facebook
track({
id: "user-signed-up",
parameters: {
provider: "facebook"
}
})
// a product was sold
track({
id: "product-sale",
parameters: {
product: product.name,
currency: customer.currency
}
})
// a page was opened
track({
id: "open-page",
parameters: {
path: parameters.path(),
screenType: parameters.screenType(),
referrer: parameters.referrer()
}
})
trackPage(options)
trackPage(options?: TrackPageOptions): TrackPageResult
options: TrackPagesOptions
Optional
Default value: {}
The options to configure the behaviour of the tracking, namely:
false
)false
)options.hash: boolean
Optional
Default value: false
true
to also track the hash portion of the URL.options.search: boolean
Optional
Default value: false
true
to also track the search portion of the URL.returns
{ stop(): void }
to cancel page tracking if needed.parameters.locale()
locale
of the current user, for example: en-US
, pt-BR
orfr-FR
.parameters.screenType()
Gets the type of screen the user is currently on, possible return values:
"XS"
if screen width <= 414px
: Mobile phone"S"
if screen width <= 800px
: Tablet"M"
if screen width <= 1200px
: Small laptop"L"
if screen width <= 1600px
: Large laptop / small desktop"XL"
if screen width > 1600px
: Large desktopparameters.referrer()
Gets the referrer of the user.
For example "https://google.com"
if the user came from Google.
parameters.path(hash?: boolean, search?: boolean)
hash?: boolean
Optional
Default value: false
true
, also returns the hash segment of the URL.search?: boolean
Optional
Default value: false
true
, also returns the search segment of the URL.