countlyCommon

countlyCommon

Source:

Object with common functions to be used for multiple purposes

Members

(static) ACTIVE_APP_ID :string|number

Source:

App ID of currently selected app or 0 when not initialized

Type:
  • string | number

(static) ACTIVE_APP_KEY :string|number

Source:

App Key of currently selected app or 0 when not initialized

Type:
  • string | number

(static) API_PARTS

Source:
Properties:
Name Type Description
data object

contains default read and write endpoints

Properties
Name Type Description
w object

path to default write endpoint /i

r object

path to default write endpoint /o

apps object

contains default read and write endpoints for managing apps

Properties
Name Type Description
w object

path to default write endpoint /i/apps

r object

path to default write endpoint /o/apps

users object

contains default read and write endpoints for managing users

Properties
Name Type Description
w object

path to default write endpoint /i/users

r object

path to default write endpoint /o/users

Main api path to make all ajax requests to. This value is configured in countly.config.js or over written through theme.

(static) API_PARTS

Source:
Properties:
Name Type Description
data object

contains default read and write endpoints

Properties
Name Type Description
w object

path to default write endpoint /i

r object

path to default write endpoint /o

apps object

contains default read and write endpoints for managing apps

Properties
Name Type Description
w object

path to default write endpoint /i/apps

r object

path to default write endpoint /o/apps

users object

contains default read and write endpoints for managing users

Properties
Name Type Description
w object

path to default write endpoint /i/users

r object

path to default write endpoint /o/users

Main api path to make all ajax requests to. This value is configured in countly.config.js or over written through theme.

(static) API_URL :string

Source:

Main api path to make all ajax requests to. Also takes into account path setting in config files, when countly is run from subdirectory. This value is configured in countly.config.js or over written through theme.

Type:
  • string

(static) API_URL :string

Source:

Main api path to make all ajax requests to. Also takes into account path setting in config files, when countly is run from subdirectory. This value is configured in countly.config.js or over written through theme.

Type:
  • string

(static) BROWSER_LANG :string

Source:

Current user's selected language in form en-EN, by default will use browser's language

Type:
  • string

(static) BROWSER_LANG_SHORT :string

Source:

Current user's browser language in short form as "en", by default will use browser's language

Type:
  • string

(static) CITY_DATA :boolean

Source:

Enable/disable displaying city level information on dashboard. default value is true or enabled. Similarly recording of city level data can be enabled/disabled on server side. This value only controls displaying data. This value is configured in countly.config.js or over written through theme.

Type:
  • boolean

(static) CITY_DATA :boolean

Source:

Enable/disable displaying city level information on dashboard. default value is true or enabled. Similarly recording of city level data can be enabled/disabled on server side. This value only controls displaying data. This value is configured in countly.config.js or over written through theme.

Type:
  • boolean

(static) DASHBOARD_IDLE_MS :number

Source:

Amount of miliseconds how long to wait for user being idle before turning off automatic dashboard refresh. Default is 3000000 or each 50 minutes. This value is configured in countly.config.js or over written through theme.

Type:
  • number

(static) DASHBOARD_IDLE_MS :number

Source:

Amount of miliseconds how long to wait for user being idle before turning off automatic dashboard refresh. Default is 3000000 or each 50 minutes. This value is configured in countly.config.js or over written through theme.

Type:
  • number

(static) DASHBOARD_REFRESH_MS :number

Source:

Amount of miliseconds on how often to refresh the dashboard. Default is 10000 or each 10 seconds. This value is configured in countly.config.js or over written through theme.

Type:
  • number

(static) DASHBOARD_REFRESH_MS :number

Source:

Amount of miliseconds on how often to refresh the dashboard. Default is 10000 or each 10 seconds. This value is configured in countly.config.js or over written through theme.

Type:
  • number

(static) DEBUG :boolean

Source:

Run countly in Debug mode without main requests to the server. Default value is false. This value is configured in countly.config.js or over written through theme.

Type:
  • boolean

(static) DEBUG :boolean

Source:

Run countly in Debug mode without main requests to the server. Default value is false. This value is configured in countly.config.js or over written through theme.

Type:
  • boolean

(static) GRAPH_COLORS :array

Source:

Array of colors to be used on graphs. This value is configured in countly.config.js or over written through theme.

Type:
  • array

(static) GRAPH_COLORS :array

Source:

Array of colors to be used on graphs. This value is configured in countly.config.js or over written through theme.

Type:
  • array

(static) periodObj

Source:
Properties:
Name Type Attributes Description
currentPeriodArr array <optional>

array with ticks for current period (available only for special periods), example ["2016.12.22","2016.12.23","2016.12.24", ...]

previousPeriodArr array <optional>

array with ticks for previous period (available only for special periods), example ["2016.12.22","2016.12.23","2016.12.24", ...]

dateString string

date format to use when outputting date in graphs, example D MMM, YYYY

isSpecialPeriod boolean

true if current period is special period, false if it is not

daysInPeriod number

amount of full days in selected period, example 30

numberOfDays number

number of days selected period consists of, example hour period has 1 day

periodContainsToday boolean

true if period contains today, false if not

uniquePeriodArr array

array with ticks for current period which contains data for unique values, like unique users, example ["2016.12.22","2016.w52","2016.12.30", ...]

uniquePeriodCheckArr array

array with ticks for higher buckets to current period unique value estimation, example ["2016.w51","2016.w52","2016.w53","2017.1",...]

previousUniquePeriodArr array

array with ticks for previous period which contains data for unique values, like unique users, example ["2016.12.22","2016.w52","2016.12.30"]

previousUniquePeriodCheckArr array

array with ticks for higher buckets to previous period unique value estimation, example ["2016.w47","2016.w48","2016.12"]

activePeriod string

period name formatted in dateString

previousPeriod string

previous period name formatted in dateString

periodMax number

max value of current period tick

periodMin number

min value of current period tick

Currently selected period

Examples

Special period object (7days)

   {
       "currentPeriodArr":["2017.1.14","2017.1.15","2017.1.16","2017.1.17","2017.1.18","2017.1.19","2017.1.20"],
       "previousPeriodArr":["2017.1.7","2017.1.8","2017.1.9","2017.1.10","2017.1.11","2017.1.12","2017.1.13"],
       "isSpecialPeriod":true,
       "dateString":"D MMM",
       "daysInPeriod":7,
       "numberOfDays":7,
       "uniquePeriodArr":["2017.1.14","2017.w3"],
       "uniquePeriodCheckArr":["2017.w2","2017.w3"],
       "previousUniquePeriodArr":["2017.1.7","2017.1.8","2017.1.9","2017.1.10","2017.1.11","2017.1.12","2017.1.13"],
       "previousUniquePeriodCheckArr":["2017.w1","2017.w2"],
       "periodContainsToday":true
   }

Simple period object (today period - hour)

   {
       "activePeriod":"2017.1.20",
       "periodMax":23,
       "periodMin":0,
       "previousPeriod":"2017.1.19",
       "isSpecialPeriod":false,
       "dateString":"HH:mm",
       "daysInPeriod":0,
       "numberOfDays":1,
       "uniquePeriodArr":[],
       "uniquePeriodCheckArr":[],
       "previousUniquePeriodArr":[],
       "previousUniquePeriodCheckArr":[],
       "periodContainsToday":true
   }

Methods

(static) arrayAddUniq(arr, item) → {array}

Source:

Add item or array to existing array only if values are not already in original array

Parameters:
Name Type Description
arr array

original array where to add unique elements

item string | number | array

item to add or array to merge

Returns:

array with unique values

Type
array

(static) browserLang() → {string}

Source:

Get Browser language

Example
//outputs en-US
countlyCommon.browserLang()
Returns:

browser locale in iso format en-US

Type
string

(static) calculateBarData(chartData) → {array}

Source:

Extracts top three items (from rangeArray) that have the biggest total session counts from the chartData.

Example

Return data

[
   {"name":"iOS","percent":35},
   {"name":"Android","percent":33},
   {"name":"Windows Phone","percent":32}
]
Parameters:
Name Type Description
chartData object

chartData retrieved from countlyCommon.extractTwoLevelData as {"chartData":[{"carrier":"At&t","t":71,"u":62,"n":36},{"carrier":"Verizon","t":66,"u":60,"n":30}]}

Returns:

array with top 3 values

Type
array

(static) decode(str) → {string}

Source:

Decode value from db, decoding first $ to $ and all . to . (dots). Decodes also url encoded values as &#36;.

Parameters:
Name Type Description
str string

value to decode

Returns:

decoded string

Type
string

(static) decodeHtml(str) → {string}

Source:

Decode escaped HTML from db

Parameters:
Name Type Description
str string

value to decode

Returns:

decoded string

Type
string

(static) divide(val1, val2) → {number}

Source:

Safe division between numbers providing 0 as result in cases when dividing by 0

Example
//outputs 0
countlyCommon.divide(100, 0);
Parameters:
Name Type Description
val1 number

number which to divide

val2 number

number by which to divide

Returns:

result of division

Type
number

(static) drawGauge(targetEl, value, maxValue, gaugeColor, textField)

Source:

Draws a gauge with provided value on procided container.

Parameters:
Name Type Description
targetEl string | object

selector for container or container object itself where to create graph

value number

value to display on gauge

maxValue number

maximal value of the gauge

gaugeColor string

color of the gauge in hexadecimal string as #ffffff

textField string | object

selector for container or container object itself where to output textual value

(static) drawGraph(dataPoints, container, graphType, inGraphProperties)

Source:

Draws a graph with the given dataPoints to container. Used for drawing bar and pie charts.

Examples

Drawing Pie chart

countlyCommon.drawGraph({"dp":[
   {"data":[[0,20]],"label":"Test1","color":"#52A3EF"},
   {"data":[[0,30]],"label":"Test2","color":"#FF8700"},
   {"data":[[0,50]],"label":"Test3","color":"#0EC1B9"}
]}, "#dashboard-graph", "pie");

Drawing bar chart, to comapre values with different color bars

//[-1,null] and [3,null] are used for offsets from left and right
countlyCommon.drawGraph({"dp":[
   {"data":[[-1,null],[0,20],[1,30],[2,50],[3,null]],"color":"#52A3EF"}, //first bar set
   {"data":[[-1,null],[0,50],[1,30],[2,20],[3,null]],"color":"#0EC1B9"} //second bar set
],
   "ticks":[[-1,""],[0,"Test1"],[1,"Test2"],[2,"Test3"],[3,""]]
}, "#dashboard-graph", "separate-bar", {"series":{"stack":null}});

Drawing Separate bars chart, to comapre values with different color bars

//[-1,null] and [3,null] are used for offsets from left and right
countlyCommon.drawGraph({"dp":[
   {"data":[[-1,null],[0,20],[1,null],[2,null],[3,null]],"label":"Test1","color":"#52A3EF"},
   {"data":[[-1,null],[0,null],[1,30],[2,null],[3,null]],"label":"Test2","color":"#FF8700"},
   {"data":[[-1,null],[0,null],[1,null],[2,50],[3,null]],"label":"Test3","color":"#0EC1B9"}
],
   "ticks":[[-1,""],[0,"Test1"],[1,"Test2"],[2,"Test3"],[3,""]
]}, "#dashboard-graph", "separate-bar");
Parameters:
Name Type Description
dataPoints object

data poitns to draw on graph

container string | object

selector for container or container object itself where to create graph

graphType string

type of the graph, accepted values are bar, line, pie, separate-bar

inGraphProperties object

object with properties to extend and use on graph library directly

(static) drawHorizontalStackedBars(data, intoElement, colorIndex)

Source:

Draws horizibtally stacked bars like in platforms and density analytic sections.

Parameters:
Name Type Description
data array

data to draw in form of [{"data":[[0,85]],"label":"Test1"},{"data":[[0,79]],"label":"Test2"},{"data":[[0,78]],"label":"Test3"}]

intoElement object | string

selector for container or container object itself where to create graph

colorIndex number

index of color from countlyCommon.GRAPH_COLORS

(static) drawTimeGraph(dataPoints, container, bucketopt, overrideBucketopt)

Source:

Draws a time line graph with the given dataPoints to container.

Example
countlyCommon.drawTimeGraph([{
   "data":[[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,12],[8,9],[9,10],[10,5],[11,8],[12,7],[13,9],[14,4],[15,6]],
   "label":"Total Sessions",
   "color":"#DDDDDD",
   "mode":"ghost"
},{
   "data":[[1,74],[2,69],[3,60],[4,17],[5,6],[6,3],[7,13],[8,25],[9,62],[10,34],[11,34],[12,33],[13,34],[14,30],[15,1]],
   "label":"Total Sessions",
   "color":"#333933"
}], "#dashboard-graph");
Parameters:
Name Type Attributes Description
dataPoints object

data poitns to draw on graph

container string | object

selector for container or container object itself where to create graph

bucket string <optional>

time bucket to display on graph. See countlyCommon.getTickObj

overrideBucket string <optional>

time bucket to display on graph. See countlyCommon.getTickObj

(static) encode(str) → {string}

Source:

Encode value to be passed to db as key, encoding $ symbol to $ if it is first and all . (dot) symbols to . in the string

Parameters:
Name Type Description
str string

value to encode

Returns:

encoded string

Type
string

(static) extendDbObj(dbObj, updateObj)

Source:

Merge standard countly metric data object, by mergin updateObj retrieved from action=refresh api requests object into dbObj. Used for merging the received data for today to the existing data while updating the dashboard.

Parameters:
Name Type Description
dbObj object

standard metric data object

updateObj object

standard metric data object retrieved from action=refresh request to last time bucket data only

(static) extractBarData(db, rangeArray, clearFunction, fetchFunction) → {array}

Source:

Extracts top three items (from rangeArray) that have the biggest total session counts from the db object.

Example

Return data

[
   {"name":"iOS","percent":35},
   {"name":"Android","percent":33},
   {"name":"Windows Phone","percent":32}
]
Parameters:
Name Type Description
db object

countly standard metric data object

rangeArray object

array of all metrics/segments to extract (usually what is contained in meta)

clearFunction function

function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs

fetchFunction function

function to fetch property, default used is function (rangeArr, dataObj) {return rangeArr;}

Returns:

array with top 3 values

Type
array

(static) extractChartData(db, clearFunction, chartData, dataProperties) → {object}

Source:

Extract single level data without metrics/segments, like total user data from users collection

Examples

Extracting total users data from users collection

countlyCommon.extractChartData(_sessionDb, countlySession.clearObject, [
     { data:[], label:"Total Users" }
 ], [
     {
         name:"t",
         func:function (dataObj) {
             return dataObj["u"]
         }
     }
 ]);
 

Returned data

{"chartDP":[
   {
       "data":[[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],[7,0],[8,0],[9,0],[10,0],[11,0],[12,0],[13,0],[14,0],[15,12]],
       "label":"Total Sessions",
       "color":"#DDDDDD",
       "mode":"ghost"
   },
   {
       "data":[[0,6],[1,14],[2,11],[3,18],[4,10],[5,32],[6,53],[7,55],[8,71],[9,82],[10,74],[11,69],[12,60],[13,17],[14,6],[15,3]],
       "label":"Total Sessions",
       "color":"#333933"
   }
 ],
 "chartData":[
   {"date":"22 Dec, 2016","pt":0,"t":6},
   {"date":"23 Dec, 2016","pt":0,"t":14},
   {"date":"24 Dec, 2016","pt":0,"t":11},
   {"date":"25 Dec, 2016","pt":0,"t":18},
   {"date":"26 Dec, 2016","pt":0,"t":10},
   {"date":"27 Dec, 2016","pt":0,"t":32},
   {"date":"28 Dec, 2016","pt":0,"t":53},
   {"date":"29 Dec, 2016","pt":0,"t":55},
   {"date":"30 Dec, 2016","pt":0,"t":71},
   {"date":"31 Dec, 2016","pt":0,"t":82},
   {"date":"1 Jan, 2017","pt":0,"t":74},
   {"date":"2 Jan, 2017","pt":0,"t":69},
   {"date":"3 Jan, 2017","pt":0,"t":60},
   {"date":"4 Jan, 2017","pt":0,"t":17},
   {"date":"5 Jan, 2017","pt":0,"t":6},
   {"date":"6 Jan, 2017","pt":12,"t":3}
 ],
 "keyEvents":[{"min":0,"max":12},{"min":0,"max":82}]
}
Parameters:
Name Type Description
db object

countly standard metric data object

clearFunction function

function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs

chartData object

prefill chart data with labels, colors, etc

dataProperties object

describing which properties and how to extract

Returns:

object to use in timeline graph with {"chartDP":chartData, "chartData":_.compact(tableData), "keyEvents":keyEvents}

Type
object

(static) extractRangeData(db, propertyName, rangeArray, explainRange) → {array}

Source:

Extract range data from standard countly metric data model

Example

Extracting session frequency from users collection

   //outputs [{"f":"First session","t":352,"percent":"88.4"},{"f":"2 days","t":46,"percent":"11.6"}]
   countlyCommon.extractRangeData(_userDb, "f", _frequencies, countlySession.explainFrequencyRange);
Parameters:
Name Type Description
db object

countly standard metric data object

propertyName string

name of the property to extract

rangeArray object

array of all metrics/segments to extract (usually what is contained in meta)

explainRange function

function to convert range/bucket index to meaningful label

Returns:

array containing extracted ranged data as [{"f":"First session","t":352,"percent":"88.4"},{"f":"2 days","t":46,"percent":"11.6"}]

Type
array

(static) extractTwoLevelData(db, rangeArray, clearFunction, dataProperties, estOverrideMetricopt) → {object}

Source:

Extract two level data with metrics/segments, like total user data from carriers collection

Examples

Extracting carriers data from carriers collection

var chartData = countlyCommon.extractTwoLevelData(_carrierDb, ["At&t", "Verizon"], countlyCarrier.clearObject, [
     {
         name:"carrier",
         func:function (rangeArr, dataObj) {
             return rangeArr;
         }
     },
     { "name":"t" },
     { "name":"u" },
     { "name":"n" }
]);

Return data

{"chartData":['
   {"carrier":"At&t","t":71,"u":62,"n":36},
   {"carrier":"Verizon","t":66,"u":60,"n":30}
]}
Parameters:
Name Type Attributes Description
db object

countly standard metric data object

rangeArray object

array of all metrics/segments to extract (usually what is contained in meta)

clearFunction function

function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs

dataProperties object

describing which properties and how to extract

estOverrideMetric object <optional>

data from total users api request to correct unique user values

Returns:

object to use in bar and pie charts with {"chartData":_.compact(tableData)}

Type
object

(static) formatDate(date, format) → {string}

Source:

Format date based on some locale settings

Example
//outputs Jan 20
countlyCommon.formatDate(moment(), "MMM D");
Parameters:
Name Type Description
date moment

moment js object

format string

format string to use

Returns:

date in formatted string

Type
string

(static) formatNumber(number) → {string}

Source:

Formats the number by separating each 3 digits with ,

Example
//outputs 1,234,567
countlyCommon.formatNumber(1234567);
Parameters:
Name Type Description
number number

number to format

Returns:

formatted number

Type
string

(static) formatSecond(second) → {string}

Source:

Parse second to standard time format

Parameters:
Name Type Description
second number

number

Returns:

return format "HH:MM:SS"

Type
string

(static) formatSecondForDP(chartDPs, labelName) → {array}

Source:

add one more column in chartDP[index].data to show string in dp for example: chartDPs = [ {color:"#88BBC8", label:"duration", data:[[0, 23], [1, 22]}], {color:"#88BBC8", label:"count", data:[[0, 3], [1, 3]}], } lable = 'duration',

will return chartDPs = [ {color:"#88BBC8", label:"duration", data:[[0, 23, "00:00:23"], [1, 22, "00:00:22"]}], {color:"#88BBC8", label:"count", data:[[0, 3], [1, 3]}], }

Parameters:
Name Type Description
chartDPs array
labelName string
Returns:

chartDPs

Type
array

(static) formatTime(timestamp) → {string}

Source:

Format duration to units of how much time have passed

Example
//outputs 47 year(s) 28 day(s) 11:54:26
countlyCommon.formatTime(1484654066);
Parameters:
Name Type Description
timestamp number

amount in seconds passed since some reference point

Returns:

formated time with how much units passed

Type
string

(static) formatTimeAgo(timestamp) → {string}

Source:

Format timestamp to twitter like time ago format with real date as tooltip and hidden data for exporting

Example
//outputs <span title="Tue, 17 Jan 2017 13:54:26">3 days ago<a style="display: none;">|Tue, 17 Jan 2017 13:54:26</a></span>
countlyCommon.formatTimeAgo(1484654066);
Parameters:
Name Type Description
timestamp number

timestamp in seconds or miliseconds

Returns:

formated time ago

Type
string

(static) getDashboardData(data, properties, unique, estOverrideMetric, clearObject) → {object}

Source:

Get calculated totals for each property, usualy used as main dashboard data timeline data without metric segments

Example
countlyCommon.getDashboardData(countlySession.getDb(), ["t", "n", "u", "d", "e", "p", "m"], ["u", "p", "m"], {u:"users"}, countlySession.clearObject);
//outputs
{
     "t":{"total":980,"prev-total":332,"change":"195.2%","trend":"u"},
     "n":{"total":402,"prev-total":255,"change":"57.6%","trend":"u"},
     "u":{"total":423,"prev-total":255,"change":"75.7%","trend":"u","isEstimate":false},
     "d":{"total":0,"prev-total":0,"change":"NA","trend":"u"},
     "e":{"total":980,"prev-total":332,"change":"195.2%","trend":"u"},
     "p":{"total":103,"prev-total":29,"change":"255.2%","trend":"u","isEstimate":true},
     "m":{"total":86,"prev-total":0,"change":"NA","trend":"u","isEstimate":true}
}
Parameters:
Name Type Description
data object

countly metric model data

properties array

array of all properties to extract

unique array

array of all properties that are unique from properties array. We need to apply estimation to them

estOverrideMetric object

using unique property as key and total_users estimation property as value for all unique metrics that we want to have total user estimation overridden

clearObject function

function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs

Returns:

dashboard data object

Type
object

(static) getDate(timestamp) → {string}

Source:

Get date from seconds timestamp

Example
//outputs 17.01.2017
countlyCommon.getDate(1484654066);
Parameters:
Name Type Description
timestamp number

timestamp in seconds or miliseconds

Returns:

formated date

Type
string

(static) getDateRange() → {string}

Source:

Getting the date range shown on the dashboard like 1 Aug - 30 Aug, using dateString property which holds the date format.

Returns:

string with formatted date range as 1 Aug - 30 Aug

Type
string

(static) getDescendantProp(obj, path, def) → {object

Source:

Fetches nested property values from an obj.

Examples

Path found

//outputs {"u":20,"t":20,"n":5}
countlyCommon.getDescendantProp({"2017":{"1":{"2":{"u":20,"t":20,"n":5}}}}, "2017.1.2", {"u":0,"t":0,"n":0});

Path not found

//outputs {"u":0,"t":0,"n":0}
countlyCommon.getDescendantProp({"2016":{"1":{"2":{"u":20,"t":20,"n":5}}}}, "2017.1.2", {"u":0,"t":0,"n":0});
Parameters:
Name Type Description
obj object

standard countly metric object

path string

dot separate path to fetch from object

def object

stub object to return if nothing is found on provided path

Returns:

fetched object from provided path

Type
object

(static) getMonths(reset) → {array}

Source:

Get array of localized short month names from moment js

Example
//outputs ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
countlyCommon.getMonths();
Parameters:
Name Type Description
reset boolean

used to reset months cache when changing locale

Returns:

array of short localized month names used in moment js MMM formatting

Type
array

(static) getOffsetCorrectionForTimestamp(inTS) → {number}

Source:

Correct timezone offset on the timestamp for current browser's timezone

Parameters:
Name Type Description
inTS number

second or milisecond timestamp

Returns:

corrected timestamp applying user's timezone offset

Type
number

(static) getPercentChange(previous, current) → {object}

Source:

Calculates the percent change between previous and current values.

Example
//outputs {"percent":"100%","trend":"u"}
  countlyCommon.getPercentChange(100, 200);
Parameters:
Name Type Description
previous number

data for previous period

current number

data for current period

Returns:

in the following format {"percent": "20%", "trend": "u"}

Type
object

(static) getPeriod() → {string|array}

Source:

Get currently selected period

Returns:

supported values are (month, 60days, 30days, 7days, yesterday, hour or [startMiliseconds, endMiliseconds] as [1417730400000,1420149600000])

Type
string | array

(static) getPeriodForAjax() → {string}

Source:

Get currently selected period that can be used in ajax requests

Returns:

supported values are (month, 60days, 30days, 7days, yesterday, hour or [startMiliseconds, endMiliseconds] as [1417730400000,1420149600000])

Type
string

(static) getPeriodObj() → {object}

Source:

Getter for period object

Returns:
Type
object

(static) getPersistentSettings()

Source:

Get user persistent settings

(static) getShortNumber(number) → {string}

Source:

Shortens the given number by adding K (thousand) or M (million) postfix. K is added only if the number is bigger than 10000, etc.

Example
//outputs 10K
countlyCommon.getShortNumber(10000);
Parameters:
Name Type Description
number number

number to shorten

Returns:

shorter representation of number

Type
string

(static) getSparklineData(data, props, clearObject) → {object}

Source:

Get total data for period's each time bucket as comma separated string to generate sparkle/small bar lines

Example
var sparkLines = countlyCommon.getSparklineData(countlySession.getDb(), {
    "total-sessions": "t",
    "new-users": "n",
    "total-users": "u",
    "total-duration": "d",
    "events": "e",
    "returning-users": function(tmp_x){return Math.max(tmp_x["u"] - tmp_x["n"], 0);},
    "avg-duration-per-session": function(tmp_x){return (tmp_x["t"] == 0) ? 0 : (tmp_x["d"] / tmp_x["t"]);},
    "avg-events": function(tmp_x){return (tmp_x["u"] == 0) ? 0 : (tmp_x["e"] / tmp_x["u"]);}
}, countlySession.clearObject);
//outputs
{
  "total-sessions":"73,84,80,72,61,18,11,7,17,27,66,39,41,36,39,36,6,11,6,16,22,30,33,34,32,41,29,9,2,2",
  "new-users":"24,30,25,20,16,18,11,7,17,18,20,18,17,11,15,15,6,11,6,16,13,14,12,10,7,4,8,9,2,2",
  "total-users":"45,54,50,44,37,18,11,7,17,27,36,39,41,36,39,36,6,11,6,16,22,30,33,34,32,29,29,9,2,2",
  "total-duration":"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
  "events":"73,84,80,72,61,18,11,7,17,27,66,39,41,36,39,36,6,11,6,16,22,30,33,34,32,41,29,9,2,2",
  "returning-users":"21,24,25,24,21,0,0,0,0,9,16,21,24,25,24,21,0,0,0,0,9,16,21,24,25,25,21,0,0,0",
  "avg-duration-per-session":"0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0",
  "avg-events":"1.6222222222222222,1.5555555555555556,1.6,1.6363636363636365,1.6486486486486487,1,1,1,1,1,1.8333333333333333,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1.4137931034482758,1,1,1,1"
}
Parameters:
Name Type Description
data object

countly metric model data

props object

object where key is output property name and value could be string as key from data object or function to create new value based on existing ones

clearObject function

function to prefill all expected properties as u, t, n, etc with 0, so you would not have null in the result which won't work when drawing graphs

Returns:

object with sparkleline data for each property

Type
object

(static) getTickObj(bucket, overrideBucket) → {object}

Source:

Get Date graph ticks

Example

Example output

{
  "min":0,
  "max":29,
  "tickTexts":["22 Dec, Thursday","23 Dec, Friday","24 Dec, Saturday","25 Dec, Sunday","26 Dec, Monday","27 Dec, Tuesday","28 Dec, Wednesday",
       "29 Dec, Thursday","30 Dec, Friday","31 Dec, Saturday","1 Jan, Sunday","2 Jan, Monday","3 Jan, Tuesday","4 Jan, Wednesday","5 Jan, Thursday",
      "6 Jan, Friday","7 Jan, Saturday","8 Jan, Sunday","9 Jan, Monday","10 Jan, Tuesday","11 Jan, Wednesday","12 Jan, Thursday","13 Jan, Friday",
       "14 Jan, Saturday","15 Jan, Sunday","16 Jan, Monday","17 Jan, Tuesday","18 Jan, Wednesday","19 Jan, Thursday","20 Jan, Friday"],
  "ticks":[[1,"23 Dec"],[4,"26 Dec"],[7,"29 Dec"],[10,"1 Jan"],[13,"4 Jan"],[16,"7 Jan"],[19,"10 Jan"],[22,"13 Jan"],[25,"16 Jan"],[28,"19 Jan"]]
}
Parameters:
Name Type Description
bucket string

time bucket, accepted values, hourly, weekly, monthly

overrideBucket boolean

override existing bucket logic and simply use current date for generating ticks

Returns:

object containing tick texts and ticks to use on time graphs

Type
object

(static) getTime(timestamp) → {string}

Source:

Get time from seconds timestamp

Example
//outputs 13:54
countlyCommon.getTime(1484654066);
Parameters:
Name Type Description
timestamp number

timestamp in seconds or miliseconds

Returns:

formated time

Type
string

(static) mergeMetricsByName(chartData, metric) → {object}

Source:

Merge metric data in chartData returned by @{link countlyCommon.extractChartData} or @{link countlyCommon.extractTwoLevelData }, just in case if after data transformation of countly standard metric data model, resulting chartData contains duplicated values, as for example converting null, undefined and unknown values to unknown

Examples

Sample input

   {"chartData":[
       {"metric":"Test","t":71,"u":62,"n":36},
       {"metric":"Test1","t":66,"u":60,"n":30},
       {"metric":"Test","t":2,"u":3,"n":4}
   ]}

Sample output

   {"chartData":[
       {"metric":"Test","t":73,"u":65,"n":40},
       {"metric":"Test1","t":66,"u":60,"n":30}
   ]}
Parameters:
Name Type Description
chartData object

chartData returned by @{link countlyCommon.extractChartData} or @{link countlyCommon.extractTwoLevelData }

metric string

metric name to merge

Returns:

chartData object with same metrics summed up

Type
object

(static) pad(n, width, z) → {string}

Source:

Pad number with specified character from left to specified length ,

Example
//outputs 0012
countlyCommon.pad(12, 4, "0");
Parameters:
Name Type Description
n number

number to pad

width number

pad to what length in symboles

z string

character to pad with, default 0

Returns:

padded number

Type
string

(static) round(num, digits) → {number}

Source:

Round to provided number of digits

Example
//outputs 1.235
countlyCommon.round(1.2345, 3);
Parameters:
Name Type Description
num number

number to round

digits number

amount of digits to round to

Returns:

rounded number

Type
number

(static) setActiveApp(appId)

Source:

Change currently selected app by app ID

Parameters:
Name Type Description
appId string

new app ID from @{countlyGlobal.apps} object

(static) setPeriod(period)

Source:

Change currently selected period

Parameters:
Name Type Description
period string | array

new period, supported values are (month, 60days, 30days, 7days, yesterday, hour or [startMiliseconds, endMiliseconds] as [1417730400000,1420149600000])

(static) setPersistentSettings(data)

Source:

Set user persistent settings to store local storage

Parameters:
Name Type Description
data object

Object param for set new data

(static) timeString(timestamp) → {string}

Source:

Format duration into highest unit of how much time have passed. Used in big numbers

Example
//outputs 2824.7 yrs
countlyCommon.timeString(1484654066);
Parameters:
Name Type Description
timestamp number

amount in seconds passed since some reference point

Returns:

formated time with how much highest units passed

Type
string

(static) toFirstUpper(str) → {string}

Source:

Convert string to first letter uppercase and all other letters - lowercase for each word

Example
//outputs Hello World
countlyCommon.toFirstUpper("hello world");
Parameters:
Name Type Description
str string

string to convert

Returns:

converted string

Type
string

(static) union(x, y) → {array}

Source:

Joined 2 arrays into one removing all duplicated values

Example
//outputs [1,2,3]
countlyCommon.union([1,2],[2,3]);
Parameters:
Name Type Description
x array

first array

y array

second array

Returns:

new array with only unique values from x and y

Type
array