@segment/analytics.js-core
Version:
The hassle-free way to integrate analytics into any web application.
49 lines (48 loc) • 1.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/*
* 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
*/
function pageDefaults() {
return {
path: canonicalPath(),
referrer: document.referrer,
search: location.search,
title: document.title,
url: canonicalUrl(location.search)
};
}
/**
* Return the canonical path for the page.
*/
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.
*/
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);
}
/*
* Exports.
*/
module.exports = pageDefaults;