File: routes/lib/tracker/sourceIdentification.js
/**
@module Tracker
*/
var common = require('../common'),
urlHelper = require('url');
var getRefererInformation = function (refererUrl, currentUrl, idSite)
{
/**
@class getRefererInformation
@constructor
@params refererUrl {String} The document.referrer of visited url
@params currentUrl {String} The current visiting url
@params Id {Number} Profile Id of registerd website.
@getRefererInformation class provides get the referrer information of the visited url.
*/
this.idsite = idSite;
this.CONSTANTS = require('./constants');
// default values for the referer_* fields
this.refererUrl = refererUrl;
this.currentUrl = currentUrl;
this.refererUrlParse = '';
this.currentUrlParse = '';
this.typeRefererAnalyzed ='';
this.nameRefererAnalyzed = '';
this._det = {utm_source:'',utm_term:'',utm_medium:'',utm_content:'',utm_campaign:''};
this.refererHost = '';
this.init();
}
getRefererInformation.prototype.init = function(){
/**
@method init
@initializes the class variables.
@return {Void} .
*/
this.refererUrl = this.unsanitizeInputValue(this.refererUrl);
this.refererUrlParse = common.isset(this.refererUrl)? urlHelper.parse(this.refererUrl,1):undefined;
this.currentUrlParse = common.isset(this.currentUrl)? urlHelper.parse(this.currentUrl,1):undefined;
this.nameRefererAnalyzed=this.CONSTANTS.REFERER_TYPE_DIRECT_ENTRY;
this.refererHost = common.isset(this.refererUrlParse)?this.refererUrlParse.host:'';
}
getRefererInformation.prototype.unsanitizeInputValue = function(refererUrl){
/**
@method unsanitizeInputValue
@return {String} url with unwanted things removed.
*/
return refererUrl;
}
getRefererInformation.prototype.isParams=function (url)
{
/**
@method isParams
@params url the url
@return {Boolean} of Query string variables as object.
*/
return ( url.split('?')[1] == undefined) ? false : true;
}
getRefererInformation.prototype.getRefererObject=function (){
/**
* using to getRefererInformation from url.
@method getRefererObject
@return {object} returns following format
{
'RefererType' : value,
'RefererName' : value,
'RefererKeyword' : value,
'RefererUrl' : value
}
*/
//check campaign,check direct, check
if(common.isset(this.refererUrlParse) && common.isset(this.refererUrlParse['host']))
{
this.refererHost = this.refererUrlParse['host'];
}
var refererDetected = false;
var campaign = this.detectRefererCampaign();
if( !common.empty(this.currentUrlParse['host']) && campaign )
{
refererDetected = true;
this._det = campaign;
this.typeRefererAnalyzed = this.CONSTANTS.REFERER_TYPE_CAMPAIGN_ENTRY;
}
if(!refererDetected && !common.empty(this.refererHost) )
{
if(this.detectRefererSearchEngine()){
refererDetected = true;
this.typeRefererAnalyzed = this.CONSTANTS.REFERER_TYPE_ORGANIC_ENTRY;
this.wrapOrganicUtm();
}
if(!refererDetected)
{
refererDetected = true;
this.typeRefererAnalyzed = this.CONSTANTS.REFERER_TYPE_REFERRAL_ENTRY;
this.wrapReferralUtm();
}
}
if( this.detectRefererDirectEntry() && !refererDetected)
{
this.typeRefererAnalyzed = this.CONSTANTS.REFERER_TYPE_DIRECT_ENTRY;
this.wrapDirectUtm();
}
return {
'RefererType' : this.typeRefererAnalyzed,
'refererUrlParse' : this.refererUrlParse,
'currentUrlParse' : this.currentUrlParse,
'utm' : this._det
};
}
getRefererInformation.prototype.wrapDirectUtm = function(){
/**
* wraping up the utm parameters
* It will set the utm details of the current visit
@method wrapDirectUtm
*/
this._det.utm_medium = this.typeRefererAnalyzed;
this._det.utm_source= this.refererHost;
}
getRefererInformation.prototype.wrapReferralUtm = function(){
/**
* wraping up the utm parameters
* It will set the utm details of the current visit
@method wrapReferalUtm
*/
this._det.utm_medium = this.typeRefererAnalyzed;
this._det.utm_source= this.refererHost;
}
getRefererInformation.prototype.wrapOrganicUtm = function(){
/**
* wraping up the utm parameters
* It will set the utm details of the current visit
@method wrapOrganicUtm
*/
this._det.utm_medium = this.typeRefererAnalyzed;
this._det.utm_source= this.refererHost;
if(common.isset(this.refererUrlParse.query.q))
this._det.utm_term = this.refererUrlParse.query.q;
}
getRefererInformation.prototype.detectRefererCampaign = function()
{
/**
* using detect campaign for current visit.
* not implimented.
@method detectRefererCampaign
@return {Boolean}.
*/
if (!common.empty(this.currentUrlParse['query']) && !common.isset(this.currentUrlParse['fragment'])) {
return false;
}
this.campaignParameters = this.getCampaignParameters();
/*
this.campaignNames = campaignParameters[0];
this.campaignKeywords = campaignParameters[1];
*/
var found = false;
found = this.detectCampaign();
return found;
}
getRefererInformation.prototype.getCampainFromUrl = function(campaignNames,parameters) {
/**
* using detect campaign for current url parameters.
* not implimented.
@method getCampainFromUrl
@params campaignNames {Array}, params{Array}
@return {Array}.
*/
var campaign=[];
for ( var campaignNameParameter in campaignNames ) {
if(parameters[campaignNameParameter])
campaign[campaignNameParameter] = parameters[campaignNameParameter];
}
return campaign;
}
getRefererInformation.prototype.detectCampaign = function(){
/**
* using detect campaign for current url parameters.
* not implimented
@method detectCampaign
@return {Object} this contains array of campaigns as key value pairs
*/
var campaign = this.getCampainFromUrl(this.campaignNames,this.currentUrlParse);
if(campaign.length == 0 )
campaign = this.getCampainFromUrl(this.campaignNames,this.currentreferralParse);
return campaign;
}
getRefererInformation.prototype.detectRefererSearchEngine=function()
{
/**
* Search engine detection not implemented
* @return bool
*/
return common.isset(this.refererUrlParse) && common.isset(this.refererUrlParse.host) && this.refererUrlParse.host.search('google') > -1;
}
getRefererInformation.prototype.getCampaignParameters = function()
{
/**
@method getCampaignParameters
@return {Array} hostname of the url.
@to get the host name of the current url.
*/
return ['utm_source','utm_term','utm_medium','utm_content','utm_campaign','gclid'];
}
getRefererInformation.prototype.detectRefererDirectEntry = function()
{
/**
@method detectRefererDirectEntry
@return {Boolean} .
*/
return !common.isset(this.refererUrlParse);
}
//exporting.
module.exports.getRefererInformation = exports.getRefererInformation = getRefererInformation;