viakite
Dashboard

SDKs/Web/pageview()

pageview()

Send a page view by hand

pageview() sends a page view by hand. You need it only when you turn automatic page views off, for a router that doesn't use the browser's history API, or to choose yourself what counts as a page.

Signature

ts
1function pageview(options?: PageviewOptions): void;
2
3interface PageviewOptions {
4 notFound?: boolean;
5}

Parameters

options.notFoundbooleandefault false

This page is a not-found page: the view counts as a broken page.

The page view is for the page the browser shows when you call it: its URL is location.href, cleaned the same way as automatic page views.

Examples

Turn automatic page views off in init(), then call pageview() on the first load and on every route change:

src/main.ts
1import { init, pageview } from "@viakite/sdk";
2import { router } from "./router";
3
4init({ project: "pk_live_us_…", autoPageviews: false });
5
6pageview();
7router.afterEach(() => pageview());

On a not-found route, in place of the plain call:

src/routes/not-found.ts
1import { pageview } from "@viakite/sdk";
2
3pageview({ notFound: true });

With a component, pass the prop instead: <Analytics project="pk_live_us_…" autoPageviews={false} />.

npm only

pageview() is not on the script tag's window.viakite: the script tag always sends page views itself. To mark a not-found page with the script tag, add data-404 to the script on that page.

How it works

  1. It sends the engaged time and scroll depth of the page view before it, if there is enough to send.
  2. It starts measuring the new page view from zero.
  3. It sends the page view at once, with the URL, the screen size, the browser's language, the title (when title is on) and, on the first page view only, the referrer.

With autoPageviews: false, init() sends no page view and doesn't follow page changes; every page view, the first one included, comes from your calls. Engaged time, scroll depth, outbound links, file downloads and class events still work.

With automatic page views on, don't call pageview() on route changes: the SDK already sends one for each new path, and your call sends a second.

Called before init(), it waits and runs once init() does. Each call sends one page view; it doesn't check whether the path changed.

The not-found flag

There are two ways to mark a broken page:

  • notFound: true in init() (or data-404, or the notFound prop) marks the first page view only. Use it on a static 404 page.
  • pageview({ notFound: true }) marks that one page view. Use it in a single-page app with autoPageviews: false, from the not-found route, in place of the plain pageview() call for that route.

Marked page views show in Analytics → Broken pages in the dashboard, with the pages that linked to them.

Warnings in debug mode

pageview() never throws. With debug on, each page view is a [viakite] send {…} line in the console, and nothing is sent.

Next steps