UNPKG

@clearbit-dcp/clearbit.js-core

Version:

The hassle-free way to integrate analytics into any web application.

78 lines (65 loc) 1.52 kB
'use strict'; /* * Module dependencies. */ var canonical = require('@segment/canonical'); var includes = require('@ndhoule/includes'); var url = require('component-url'); /** * Return a default `options.context.page` object. * * https://segment.com/docs/spec/page/#properties * * @return {Object} */ function pageDefaults() { return { path: canonicalPath(), referrer: document.referrer, search: searchPath(), title: document.title, url: canonicalUrl(location.search) }; } /** * Return the canonical path for the page. * * @return {string} */ function canonicalPath() { var canon = canonical(); if (!canon) return window.location.pathname; var parsed = url.parse(canon); return parsed.pathname; } /** * Return the canonical URL for the page concat the given `search` * and strip the hash. * * @param {string} search * @return {string} */ function canonicalUrl(search) { var canon = canonical(); if (canon) return includes('?', canon) ? canon : canon + search; var url = window.location.href; var i = url.indexOf('#'); return i === -1 ? url : url.slice(0, i); } /** * Return the search path for the page * This is preferable to window.location.search because SPAs * can have a # in the url which will include the query param in * the hash * * @return {string} */ function searchPath() { var url = window.location.href; var u = url.indexOf('?'); return u === -1 ? '' : url.slice(u, -1); } /* * Exports. */ module.exports = pageDefaults;