A node module for Google\textquotesingle{}s \href{http://support.google.com/analytics/bin/answer.py?hl=en&hlrm=de&answer=2790010}{\texttt{ Universal Analytics}} tracking via the \href{https://developers.google.com/analytics/devguides/collection/protocol/v1/}{\texttt{ Measurement Protocol}}.

This module allows tracking data (or rather, users) from within a Node.\+js application. Tracking is initiated on the server side and, if required, does not require any more tracking in the browser.

\href{https://www.npmjs.com/package/universal-analytics}{\texttt{ }} \href{https://travis-ci.org/peaksandpies/universal-analytics}{\texttt{ }}\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31459}{}\doxysection{Table of Contents}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31459}

\begin{DoxyItemize}
\item \href{\#getting-started}{\texttt{ Getting started}}
\item \href{\#tracking}{\texttt{ Tracking}}
\begin{DoxyItemize}
\item \href{\#pageview-tracking}{\texttt{ Pageviews}}
\item \href{\#screenview-tracking}{\texttt{ Screenviews}}
\item \href{\#event-tracking}{\texttt{ Events}}
\item \href{\#exception-tracking}{\texttt{ Exceptions}}
\item \href{\#user-timing-tracking}{\texttt{ User timings}}
\item \href{\#transaction-tracking}{\texttt{ Transactions}}
\begin{DoxyItemize}
\item \href{\#transaction-item-tracking}{\texttt{ Transaction items}}
\end{DoxyItemize}
\end{DoxyItemize}
\item \href{\#daisy-chaining-tracking-calls}{\texttt{ Daisy-\/chaining tracking calls}}
\item \href{\#setting-persistent-parameters}{\texttt{ Setting persistent parameters}}
\item \href{\#filter-application-tracking-data}{\texttt{ Filter application tracking data}}
\item \href{\#session-based-identification}{\texttt{ Session-\/based identification}}
\item \href{\#debug-mode}{\texttt{ Debug mode}}
\item \href{\#request-options}{\texttt{ Request Options}}
\item \href{\#shortcuts}{\texttt{ Shortcuts}}
\item \href{\#tests}{\texttt{ Tests}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31460}{}\doxysection{Getting started}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31460}
{\ttfamily universal-\/analytics} is installed and included like any other node module\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install universal-\/analytics }

\end{DoxyCode}



\begin{DoxyCode}{0}
\DoxyCodeLine{var ua = require('universal-\/analytics');}
\DoxyCodeLine{}
\DoxyCodeLine{// Or with ES6 import}
\DoxyCodeLine{import ua from 'universal-\/analytics' }

\end{DoxyCode}


Initialization expects at least your Google Analytics account ID\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX'); }

\end{DoxyCode}


This will create a {\ttfamily universal-\/analytics} Visitor instance that you can use and keep around to track a specific client (Not to be confused with the Google Analytics User ID, see \href{\#setting-persistent-parameters}{\texttt{ Setting persistent parameters}} for more information on that). Since no client ID was specified in the constructor\textquotesingle{}s arguments, a random UUID is generated. In case you have a client ID at hand, you can use that to create the visitor\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX', '6a14abda-\/6b12-\/4578-\/bf66-\/43c754eaeda9'); }

\end{DoxyCode}


Starting with Universal Analytics, a UUID v4 is the preferred client ID format. It is therefor necessary to provide a UUID of such type to {\ttfamily universal-\/analytics}. However you can force custom client ID, passing {\ttfamily strict\+Cid\+Format\+: false} in the options\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX', 'CUSTOM\_CLIENTID\_1', \{strictCidFormat: false\}); }

\end{DoxyCode}


If you want to use Google Analytics in http protocol, just include it in the options {\ttfamily http\+: true}, by default will use https\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX', \{http: true\}); }

\end{DoxyCode}


If you want to set User Id you can add it into options\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX', \{uid: 'as8eknlll'\}); }

\end{DoxyCode}
 \href{https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters\#uid}{\texttt{ see about User Id}}

Tracking a pageview without much else is now very simple\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/"{}).send() }

\end{DoxyCode}


The first argument for the pageview method is the path of the page to be tracked. Simply calling {\ttfamily pageview()} will not initiate a tracking request. In order to send off tracking to the Google Analytics servers you have two options\+:


\begin{DoxyEnumerate}
\item You can append a {\ttfamily send()} call after {\ttfamily pageview()}. The tracking request is sent asynchronously. This means you will not receive any confirmation when and if it was successful.
\item You can provide a callback function to {\ttfamily pageview()} as an additional, last argument. This callback will be invoked once the tracking request has finished. Any error that occured during the request will be provided to said callback. In that case {\ttfamily send()} is no longer necessary.
\end{DoxyEnumerate}

An example of the callback approach\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX');}
\DoxyCodeLine{visitor.pageview("{}/"{}, function (err) \{}
\DoxyCodeLine{  // Handle the error if necessary.}
\DoxyCodeLine{  // In case no error is provided you can be sure}
\DoxyCodeLine{  // the request was successfully sent off to Google.}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31461}{}\doxysection{Tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31461}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31462}{}\doxysubsection{Pageview tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31462}
The first argument for the pageview tracking call is the page path. Furthermore, pageview tracking can be improved with the additional parameters to provide the page\textquotesingle{}s hostname and title to Google Analytics. The parameters are provided as arguments after the page path.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/"{}, "{}http://peaksandpies.com"{}, "{}Welcome"{}).send(); }

\end{DoxyCode}


The following snippet is the exact same tracking using a callback. It is always the last argument.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/"{}, "{}http://peaksandpies.com"{}, "{}Welcome"{}, function (err) \{}
\DoxyCodeLine{  // …}
\DoxyCodeLine{\}); }

\end{DoxyCode}


Depending on how you integrate tracking into your app, you might be more comfortable with providing all the tracking data via a params object to the {\ttfamily pageview()} method\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview(\{dp: "{}/"{}, dt: "{}Welcome"{}, dh: "{}http://peaksandpies.com"{}\}).send(); }

\end{DoxyCode}


This code has the exact same effect as the one above. {\ttfamily dp}, {\ttfamily dt}, and {\ttfamily dh} (as in \textquotesingle{}document path\textquotesingle{}, \textquotesingle{}document title\textquotesingle{} and \textquotesingle{}document hostname\textquotesingle{}) are the attribute names used by the Measurement Protocol.

It\textquotesingle{}s mandatory to specify either the page path ({\ttfamily dp}) or document location ({\ttfamily dl}). Google Analytics can not track a pageview without a path. To avoid such erroneous requests, {\ttfamily universal-\/analytics} will deny {\ttfamily pageview()} tracking if the required parameters are omitted.


\begin{DoxyCode}{0}
\DoxyCodeLine{var pagePath = null;}
\DoxyCodeLine{}
\DoxyCodeLine{visitor.pageview(pagePath, function (err) \{}
\DoxyCodeLine{  // This callback will receive an error}
\DoxyCodeLine{\}); }

\end{DoxyCode}


The following method signatures are available for the {\ttfamily pageview()} method of the Visitor instance\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#pageview(path)}
\item {\ttfamily Visitor\#pageview(path, callback)}
\item {\ttfamily Visitor\#pageview(params)}
\item {\ttfamily Visitor\#pageview(params, callback)}
\item {\ttfamily Visitor\#pageview(path, hostname)}
\item {\ttfamily Visitor\#pageview(path, hostname, callback)}
\item {\ttfamily Visitor\#pageview(path, hostname, title)}
\item {\ttfamily Visitor\#pageview(path, hostname, title, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31463}{}\doxysubsection{Screenview tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31463}
Instead of pageviews app will want to track screenviews.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.screenview("{}Home Screen"{}, "{}App Name"{}).send() }

\end{DoxyCode}


The following method signatures are available for \#screenview\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, callback)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, callback)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, app\+Id)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, app\+Id, callback)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, app\+Id, app\+Installer\+Id)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, app\+Id, app\+Installer\+Id, callback)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, app\+Id, app\+Installer\+Id, params)}
\item {\ttfamily Visitor\#screenview(screen\+Name, app\+Name, app\+Version, app\+Id, app\+Installer\+Id, params, callback)}
\item {\ttfamily Visitor\#screenview(params)}
\item {\ttfamily Visitor\#screenview(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31464}{}\doxysubsection{Event tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31464}
Tracking events with {\ttfamily universal-\/analytics} works like pageview tracking, only you have to provide different arguments\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.event("{}Event Category"{}, "{}Event Action"{}).send() }

\end{DoxyCode}


This is the most straightforward way to track an event. The event attributes {\itshape label} and {\itshape value} are optional and can be provided if necessary\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.event("{}Event Category"{}, "{}Event Action"{}, "{}…and a label"{}, 42).send() }

\end{DoxyCode}


Just like pageview tracking, event tracking supports a callback as the last argument\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.event("{}Event Category"{}, "{}Event Action"{}, "{}…and a label"{}, 42, function (err) \{}
\DoxyCodeLine{  // …}
\DoxyCodeLine{\}) }

\end{DoxyCode}


An additional attribute for events is the path of the page they should be associated with in Google Analytics. You can provide this path via an additional params object\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.event("{}Event Category"{}, "{}Event Action"{}, "{}…and a label"{}, 42, \{p: "{}/contact"{}\}, function (err) \{}
\DoxyCodeLine{  // …}
\DoxyCodeLine{\}) }

\end{DoxyCode}


{\itshape Notice\+:} The page path attribute for the event is called {\ttfamily p} which differs from the {\ttfamily dp} attribute used in the pageview tracking example. {\ttfamily universal-\/analytics} is smart enough to use the {\ttfamily dp} attribute should you provide it instead of {\ttfamily p}.

In case this argument list is getting a little long, {\ttfamily event()} also accepts a params object like {\ttfamily pageview()}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var params = \{}
\DoxyCodeLine{  ec: "{}Event Category"{},}
\DoxyCodeLine{  ea: "{}Event Action"{},}
\DoxyCodeLine{  el: "{}…and a label"{},}
\DoxyCodeLine{  ev: 42,}
\DoxyCodeLine{  dp: "{}/contact"{}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{visitor.event(params).send(); }

\end{DoxyCode}


The category ({\ttfamily ec}) and the action ({\ttfamily ea}) are mandatory. Google Analytics will not track an event without them. To avoid such erroneous requests, universal-\/analytics will deny {\ttfamily event()} tracking if either attribute is omitted.


\begin{DoxyCode}{0}
\DoxyCodeLine{var action = null;}
\DoxyCodeLine{}
\DoxyCodeLine{visitor.event("{}Navigation clicks"{}, action, function (err) \{}
\DoxyCodeLine{  // This callback will receive an error}
\DoxyCodeLine{\}); }

\end{DoxyCode}


The following method signatures are available for \#event\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#event(category, action)}
\item {\ttfamily Visitor\#event(category, action, callback)}
\item {\ttfamily Visitor\#event(category, action, label)}
\item {\ttfamily Visitor\#event(category, action, label, callback)}
\item {\ttfamily Visitor\#event(category, action, label, value)}
\item {\ttfamily Visitor\#event(category, action, label, value, callback)}
\item {\ttfamily Visitor\#event(category, action, label, value, params, callback)}
\item {\ttfamily Visitor\#event(params)}
\item {\ttfamily Visitor\#event(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31465}{}\doxysubsection{E-\/commerce tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31465}
E-\/commerce tracking in general is a bit more complex. It requires a combination of one call to the {\ttfamily transaction()} method and one or more calls to the {\ttfamily item()} method.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor}
\DoxyCodeLine{  .transaction("{}trans-\/12345"{}, 500)   // Create transaction trans-\/12345 worth 500 total.}
\DoxyCodeLine{  .item(300, 1, "{}item-\/54321"{})        // Add 1 unit the item item-\/54321 worth 300.}
\DoxyCodeLine{  .item(200, 2, "{}item-\/41325"{})        // Add 2 units the item item-\/41325 worth 200.}
\DoxyCodeLine{  .send() }

\end{DoxyCode}


Once again, daisy-\/chaining simplifies associating the items with the transaction. Officially, nothing but the transaction ID is a requirement for both the transaction and the items. However, providing a minimum set of information (revenue for the transaction, price, quantity and ID for the items) is recommended.

It is also possible to provide the params as an object to both methods\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor}
\DoxyCodeLine{  .transaction(\{ti: "{}trans-\/12345"{}, tr: 500, ts: 50, tt: 100, ta: "{}Partner 13"{}\})}
\DoxyCodeLine{  .item(\{ip: 300, iq: 1, ic: "{}item-\/54321"{}, in: "{}Item 54321"{}, iv: "{}Blue"{}\})}
\DoxyCodeLine{  .item(\{ip: 200, iq: 2, ic: "{}item-\/41325"{}, in: "{}Item 41325"{}, iv: "{}XXL"{}\})}
\DoxyCodeLine{  .send() }

\end{DoxyCode}


In case an additional item has to be added later on or daisy-\/chaining is not available for another reason, each item can be given an associated transaction ID via the params object as well\+:

visitor.\+item(\{ip\+: 100, iq\+: 1, ic\+: \char`\"{}item-\/41325\char`\"{}, in\+: \char`\"{}\+Item 41325\char`\"{}, iv\+: \char`\"{}\+XL\char`\"{}, ti\+: \char`\"{}trans-\/12345\char`\"{}\}).send()

The transaction ID ({\ttfamily ti}) is mandatory for both the transaction and the item. Google Analytics will not track e-\/commerce data without it. To avoid such erroneous requests, universal-\/analytics will deny {\ttfamily transaction()} and {\ttfamily item()} tracking if it is omitted.


\begin{DoxyCode}{0}
\DoxyCodeLine{var ti = null;}
\DoxyCodeLine{}
\DoxyCodeLine{visitor.transaction(ti, function (err) \{}
\DoxyCodeLine{  // This callback will receive an error}
\DoxyCodeLine{\}); }

\end{DoxyCode}


The following method signatures are available for \#transaction\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#transaction(id)}
\item {\ttfamily Visitor\#transaction(id, callback)}
\item {\ttfamily Visitor\#transaction(id, revenue)}
\item {\ttfamily Visitor\#transaction(id, revenue, callback)}
\item {\ttfamily Visitor\#transaction(id, revenue, shipping)}
\item {\ttfamily Visitor\#transaction(id, revenue, shipping, callback)}
\item {\ttfamily Visitor\#transaction(id, revenue, shipping, tax)}
\item {\ttfamily Visitor\#transaction(id, revenue, shipping, tax, callback)}
\item {\ttfamily Visitor\#transaction(id, revenue, shipping, tax, affiliation)}
\item {\ttfamily Visitor\#transaction(id, revenue, shipping, tax, affiliation, callback)}
\item {\ttfamily Visitor\#transaction(params)}
\item {\ttfamily Visitor\#transaction(params, callback)}
\end{DoxyItemize}

The following method signatures are available for \#item\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#item(price)}
\item {\ttfamily Visitor\#item(price, callback)}
\item {\ttfamily Visitor\#item(price, quantity)}
\item {\ttfamily Visitor\#item(price, quantity, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku)}
\item {\ttfamily Visitor\#item(price, quantity, sku, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation, params)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation, params, callback)}
\item {\ttfamily Visitor\#item(params)}
\item {\ttfamily Visitor\#item(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31466}{}\doxysubsection{Exception tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31466}
Exception tracking is a way to keep track of any sort of application errors and bugs with Google Analytics. Using it with this module is a way to capture server-\/side problems.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.exception("{}StackOverflow Error"{}).send() }

\end{DoxyCode}


As an additional information, the exception can be flagged as fatal if the error was exceptionally bad.


\begin{DoxyCode}{0}
\DoxyCodeLine{var fatal = true;}
\DoxyCodeLine{visitor.exception("{}StackOverflow Error"{}, fatal, function () \{}
\DoxyCodeLine{  // Finish handling this error}
\DoxyCodeLine{\}); }

\end{DoxyCode}


The following method signatures are available for \#exception\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#exception(description)}
\item {\ttfamily Visitor\#exception(description, callback)}
\item {\ttfamily Visitor\#exception(description, fatal)}
\item {\ttfamily Visitor\#exception(description, fatal, callback)}
\item {\ttfamily Visitor\#exception(params)}
\item {\ttfamily Visitor\#exception(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31467}{}\doxysubsection{User timing tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31467}
Tracking user timings is a way to capture time-\/based information similar to the page load speed data tracked automatically by Google Analytics. All arguments to this tracking method are optional, but a category, a variable and a time value should be provided. The time value should be provided in milliseconds.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.timing("{}User interaction"{}, "{}Time to open login overlay"{}, 12547).send() }

\end{DoxyCode}


The following method signatures are available for \#timing\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#timing(category)}
\item {\ttfamily Visitor\#timing(category, callback)}
\item {\ttfamily Visitor\#timing(category, variable)}
\item {\ttfamily Visitor\#timing(category, variable, callback)}
\item {\ttfamily Visitor\#timing(category, variable, time)}
\item {\ttfamily Visitor\#timing(category, variable, time, callback)}
\item {\ttfamily Visitor\#timing(category, variable, time, label)}
\item {\ttfamily Visitor\#timing(category, variable, time, label, callback)}
\item {\ttfamily Visitor\#timing(params)}
\item {\ttfamily Visitor\#timing(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31468}{}\doxysubsection{Transaction tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31468}
Transactions are the main tracking calls for ecommerce tracking


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.transaction("{}123456"{}, "{}449.99"{}).send() }

\end{DoxyCode}


The following method signatures are available for \#transaction\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#transaction(transaction\+Id)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, callback)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, callback)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, callback)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, tax)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, tax, callback)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, tax, affiliation)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, tax, affiliation, callback)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, tax, affiliation, params)}
\item {\ttfamily Visitor\#transaction(transaction\+Id, revenue, shipping\+Cost, tax, affiliation, params, callback)}
\item {\ttfamily Visitor\#transaction(params)}
\item {\ttfamily Visitor\#transaction(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31469}{}\doxysubsubsection{Transaction item tracking}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31469}
Transaction consist of one or more items.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.item(449.99, 1, "{}ID54321"{}, "{}T-\/Shirt"{}, \{ti: "{}123456"{}\}).send() }

\end{DoxyCode}


The following method signatures are available for \#item\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#item(price)}
\item {\ttfamily Visitor\#item(price, callback)}
\item {\ttfamily Visitor\#item(price, quantity)}
\item {\ttfamily Visitor\#item(price, quantity, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku)}
\item {\ttfamily Visitor\#item(price, quantity, sku, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation, callback)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation, params)}
\item {\ttfamily Visitor\#item(price, quantity, sku, name, variation, params, callback)}
\item {\ttfamily Visitor\#item(params)}
\item {\ttfamily Visitor\#item(params, callback)}
\end{DoxyItemize}

See also\+: \mbox{\hyperlink{md_sfcode_node_modules_universal_analytics_AcceptableParams}{List of acceptable params}}.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31470}{}\doxysection{Daisy-\/chaining tracking calls}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31470}
We have seen basic daisy-\/chaining above when calling {\ttfamily send()} right after {\ttfamily pageview()} and {\ttfamily event()}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/"{}).send() }

\end{DoxyCode}


Every call of a tracking method returns a visitor instance you can re-\/use\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/"{}).pageview("{}/contact"{}).send() }

\end{DoxyCode}


Granted, the chance of this example actually happening in practice might be rather low.

However, {\ttfamily universal-\/analytics} is smart when it comes to daisy-\/chaining certain calls. In many cases, a {\ttfamily pageview()} call is instantly followed by an {\ttfamily event()} call to track some additional information about the current page. {\ttfamily universal-\/analytics} makes creating the connection between the two easy\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/landing-\/page-\/1"{}).event("{}Testing"{}, "{}Button color"{}, "{}Blue"{}).send() }

\end{DoxyCode}
 This is the same as two distinct tracking calls.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/landing-\/page-\/1"{}).send()}
\DoxyCodeLine{visitor.event("{}Testing"{}, "{}Button color"{}, "{}Blue"{}, \{p: "{}/landing-\/page-\/1"{}\}).send() }

\end{DoxyCode}


Daisy-\/chaining is context-\/aware and in this case placing the {\ttfamily event()} call right after the {\ttfamily pageview()} call results in the event being associated with the page path tracking in the {\ttfamily pageview()} call. Even though the attributes ({\ttfamily dp} and {\ttfamily p}) are different internally.

It also works when using a callback since the {\ttfamily this} inside the callback will be the {\ttfamily universal-\/analytics} Visitor instance\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.pageview("{}/landing-\/page-\/1"{}, function (err) \{}
\DoxyCodeLine{  if (!err) \{}
\DoxyCodeLine{    this.event("{}Testing"{}, "{}Button color"{}, "{}Blue"{}).send()}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}); }

\end{DoxyCode}


More generally, the daisy-\/chaining context keeps all parameters from the previous call around. This means in a situation where similar tracking calls are necessary tracking is simplified\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor}
\DoxyCodeLine{  .event(\{ec: "{}Mail Server"{}, ea: "{}New Team Member Notification sent"{}\})}
\DoxyCodeLine{  .event(\{ea: "{}Invitation sent"{}\})}
\DoxyCodeLine{  .send(); }

\end{DoxyCode}


In this example the event category (\char`\"{}\+Mail Server\char`\"{}) is not repeated in the second tracking call.\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31471}{}\doxysection{Setting persistent parameters}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31471}
Some parameters should be in every tracking call, such as a user ID or custom dimensions that never or hardly change. For such situations a {\ttfamily \#set(key, value)} method is available


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.set("{}uid"{}, "{}123456789"{}); }

\end{DoxyCode}


The uid parameter will be part of every tracking request of that visitor from now on.

For custom dimensions, you will not pass {\ttfamily dimension\#} rather {\ttfamily cd\#}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.set("{}cd[1-\/20]"{}, "{}123456789"{}); // [1-\/20] will be the dimension number }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31472}{}\doxysection{Filter application tracking data}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31472}
Set a persistent parameter for \href{https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters\#ds}{\texttt{ {\ttfamily Data Source}}} to {\ttfamily app} in order to mark tracking data as {\ttfamily Application}.


\begin{DoxyCode}{0}
\DoxyCodeLine{visitor.set("{}ds"{}, "{}app"{}); // Allows filtering by the 'Application?' field in GA }

\end{DoxyCode}


Then create a new view in Google Analytics of type \textquotesingle{}Application\textquotesingle{}. You will then need to filter the data for that view by creating a new filter that either includes or excludes {\ttfamily Application? Yes} (depending on if you want to show(includes) or hide(excludes) application analytics in a given view).

\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31473}{}\doxysection{Session-\/based identification}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31473}
In order to make session-\/based apps easier to work with, {\ttfamily universal-\/analytics} also provides a middleware that works in an Expressjs-\/style fashion. It will try to detect a client ID based on the {\ttfamily \+\_\+ga} cookie used by the analytics.\+js client-\/side tracking. Additionally it will store the detected client ID in the current session to recognize the visitor later.


\begin{DoxyCode}{0}
\DoxyCodeLine{var ua = require("{}universal-\/analytics"{});}
\DoxyCodeLine{var express = require("{}express"{});}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{}
\DoxyCodeLine{express.use(ua.middleware("{}UA-\/XXXX-\/Y"{}, \{cookieName: '\_ga'\})); }

\end{DoxyCode}


The middleware will attach the {\ttfamily universal analytics} visitor instance to every request ({\ttfamily req.\+visitor}).

Additionally, the module also exposes a {\ttfamily create\+From\+Session} method to create a visitor instance simply based on a session, which is helpful when working with Socket.\+io, etc. where the middleware is not used.


\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua.createFromSession(socket.handshake.session); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31474}{}\doxysection{Debug mode}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31474}
{\ttfamily universal-\/analytics} is using the \href{https://www.npmjs.com/package/debug}{\texttt{ {\ttfamily debug}}} library. It can be instructed to output information during tracking by setting the {\ttfamily DEBUG} environment variable\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{DEBUG=universal-\/analytics }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31475}{}\doxysection{Request Options}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31475}
In order to add additional options to the request a {\ttfamily request\+Options} hash can be provided as part of the constructor options. {\ttfamily unviversal-\/analytics} uses the \href{https://www.npmjs.com/package/request}{\texttt{ {\ttfamily request}}} library. Therefor \href{https://www.npmjs.com/package/request\#requestoptions-callback}{\texttt{ any option available for that library}} can be provided via the {\ttfamily request\+Options}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var visitor = ua('UA-\/XXXX-\/XX', \{}
\DoxyCodeLine{  requestOptions: \{}
\DoxyCodeLine{    proxy: "{}…"{}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31476}{}\doxysection{Shortcuts}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31476}
The tracking methods have shortcuts\+:


\begin{DoxyItemize}
\item {\ttfamily Visitor\#pv} as an alias for {\ttfamily Visitor\#pageview}
\item {\ttfamily Visitor\#e} as an alias for {\ttfamily Visitor\#event}
\item {\ttfamily Visitor\#t} as an alias for {\ttfamily Visitor\#transaction}
\item {\ttfamily Visitor\#i} as an alias for {\ttfamily Visitor\#item}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31477}{}\doxysection{Tests}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31477}
The tests are written with \href{https://github.com/visionmedia/mocha}{\texttt{ mocha}} using \href{https://github.com/visionmedia/should.js}{\texttt{ should}} and \href{https://github.com/cjohansen/Sinon.JS}{\texttt{ Sinon.\+JS}}.

Run them by executing the following commands in the {\ttfamily universal-\/analytics} directory\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install}
\DoxyCodeLine{\$ make test }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_universal_analytics_README_autotoc_md31478}{}\doxysection{License}\label{md_sfcode_node_modules_universal_analytics_README_autotoc_md31478}
(The MIT License)

Copyright (c) 2017 Peaks \& Pies GmbH $<$\href{mailto:hello@peaksandpies.com}{\texttt{ hello@peaksandpies.\+com}}$>$

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \textquotesingle{}Software\textquotesingle{}), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions\+:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED \textquotesingle{}AS IS\textquotesingle{}, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 