Skip to content

Commit

Permalink
auto page tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
prabrisha-rudder committed Nov 13, 2019
1 parent 823d272 commit 572cd54
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 16 deletions.
20 changes: 15 additions & 5 deletions rudder-client-javascript/analytics/analytics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getJSONTrimmed, generateUUID, handleError } from "./utils/utils";
import { getJSONTrimmed, generateUUID, handleError, getDefaultPageProperties } from "./utils/utils";
import { CONFIG_URL, ECommerceEvents, MAX_WAIT_FOR_INTEGRATION_LOAD, INTEGRATION_LOAD_CHECK_INTERVAL } from "./utils/constants";
import { integrations } from "./integrations";
import RudderElementBuilder from "./utils/RudderElementBuilder";
Expand Down Expand Up @@ -270,14 +270,14 @@ class Analytics {
if (name) {
rudderElement["message"]["name"] = name;
}
if (!properties) {
properties = {};
}
if (category) {
if (!properties) {
properties = {};
}
properties["category"] = category;
}
if (properties) {
rudderElement["message"]["properties"] = properties;
rudderElement["message"]["properties"] = this.getPageProperties(properties)//properties;
}

this.trackPage(rudderElement, options, callback);
Expand Down Expand Up @@ -460,6 +460,16 @@ class Analytics {
}
}

getPageProperties(properties){
let defaultPageProperties = getDefaultPageProperties();
for(let key in defaultPageProperties){
if(properties[key] === undefined){
properties[key] = defaultPageProperties[key]
}
}
return properties;
}

/**
* Clear user information
*
Expand Down
56 changes: 51 additions & 5 deletions rudder-client-javascript/analytics/dist/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,39 @@ var analytics = (function (exports) {
}
}

function getDefaultPageProperties() {
var canonicalUrl = getCanonicalUrl();
var path = canonicalUrl ? canonicalUrl.pathname : window.location.pathname;
var referrer = document.referrer;
var search = window.location.search;
var title = document.title;
var url = getUrl(search);
return {
path: path,
referrer: referrer,
search: search,
title: title,
url: url
};
}

function getUrl(search) {
var canonicalUrl = getCanonicalUrl();
var url = canonicalUrl ? canonicalUrl.indexOf('?') > -1 ? canonicalUrl : canonicalUrl + search : window.location.href;
var hashIndex = url.indexOf('#');
return hashIndex > -1 ? url.slice(0, hashIndex) : url;
}

function getCanonicalUrl() {
var tags = document.getElementsByTagName('link');

for (var i = 0, tag; tag = tags[i]; i++) {
if (tag.getAttribute('rel') === 'canonical') {
return tag.getAttribute('href');
}
}
}

//Message Type enumeration
var MessageType = {
TRACK: "track",
Expand Down Expand Up @@ -1574,16 +1607,16 @@ var analytics = (function (exports) {
rudderElement["message"]["name"] = name;
}

if (category) {
if (!properties) {
properties = {};
}
if (!properties) {
properties = {};
}

if (category) {
properties["category"] = category;
}

if (properties) {
rudderElement["message"]["properties"] = properties;
rudderElement["message"]["properties"] = this.getPageProperties(properties); //properties;
}

this.trackPage(rudderElement, options, callback);
Expand Down Expand Up @@ -1768,6 +1801,19 @@ var analytics = (function (exports) {
}
}
}
}, {
key: "getPageProperties",
value: function getPageProperties(properties) {
var defaultPageProperties = getDefaultPageProperties();

for (var key in defaultPageProperties) {
if (properties[key] === undefined) {
properties[key] = defaultPageProperties[key];
}
}

return properties;
}
/**
* Clear user information
*
Expand Down

0 comments on commit 572cd54

Please sign in to comment.