Client-side analytics utilities for Webmaker apps.
Usage
The analytics module can be used as part of an AMD module system, or on the global.
It assumes that the Google Analytics (GA) snippet and/or the Optimizely tracking code
has already been included and setup in the containing page. If it has not, all analytics
methods below will be NO-OPs and do nothing.
The event(action) pushes your custom events to the _gaq array and GA will discard any items in the array before the _setAccount. Further info.
event(action, options)
The event method is used to record custom GA events using either the old ga.js API, or the newer analytics.js API. It takes two arguments:
action - A required string that is uniquely paired with each category, and commonly used
to define the type of user interaction for the web object. The action is converted
to Title Case for consistency.
options - An optional set of extra arguments, which can include:
label - An optional string to provide additional dimensions to the event data.
value - An integer that you can use to provide numerical data about the user event.
nonInteraction - A boolean that when set to true, indicates that the event hit will
not be used in bounce-rate calculation.
NOTE: the data types of the optional properties on options are important, and mismatches
will cause values to be ignored.
Also be aware that any string (e.g., action or label) that look like an email address
will cause the event to be redacted for privacy reasons (i.e., it will show up in your stats
as “REDACTED (Potential Email Address)”;). An email address is loosely defined as any string
of the form “..@..”.
Each event will be tracked using the page’s hostname as the GA Category automatically.
Example 1: Using the old GA ga.js _gaq API
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script src="analytics.js"></script>
<script>
...
function playIntroVideo() {
analytics.event("Play", {label: "Main page welcome video"});
video.play();
}
</script>
Use the analytics.virtualPageview() method along with a URL path you fabricate in order to track clicks from users that do not lead to actual website pages on your site. For example: a modal pop-up with a form a user needs to complete to progress is equivilent to a pageview even if it’s loaded asynchronously. In general, we recommend you use analytics.event() for tracking downloads, outbound links, PDFs or similar kinds of user interactions within a page.
This is because virtual pageviews will add to your total pageview count. But if you need to track the sequentional user flow from actual pages to virtual pages, use virtual pageviews.
The virtualPageview method is used to record custom GA virtualPageviews using either the old ga.js API, or the newer analytics.js API.
It takes one argument:
virtualPagePath - A required string that is unique to the virtual pageview you want to track
NOTE: the ‘virtualPagePath’ value will be prefixed in GA reports with /virtual/ to distinguish these custom tracked ‘hits’ and to avoid clashes with existing (or potential page URLs).
Usage is the same as the event() method documented above.
action - A required string that is used in the Optimizely admin interface during experiment setup. This string must match exactly to count as a conversion in a given experiment. Unlike GA events, the Optimizely action is not converted to Title Case. Avoid using spaces.
options - An optional set of extra arguments:
valueInCents - An optional integer that can be used to track revenue in A/B and multivariate testing experiments
NOTE: the data types of the optional properties on options are important, and mismatches
will cause values to be ignored.
Include webmaker-analtics in your page as shown in the GA examples above. Either directly, or as part of a Require.js module.
Webmaker Analytics
Client-side analytics utilities for Webmaker apps.
Usage
The
analyticsmodule can be used as part of an AMD module system, or on the global. It assumes that the Google Analytics (GA) snippet and/or the Optimizely tracking code has already been included and setup in the containing page. If it has not, all analytics methods below will be NO-OPs and do nothing.The module is also installable via Bower:
You then include the installed script like so:
Load order
If firing an
analytics.event(action)on pageload, this should happen after the GA script_setAccounthas been declared.The
event(action)pushes your custom events to the_gaqarray and GA will discard any items in the array before the_setAccount. Further info.event(action, options)
The
eventmethod is used to record custom GA events using either the old ga.js API, or the newer analytics.js API. It takes two arguments:action- A required string that is uniquely paired with each category, and commonly used to define the type of user interaction for the web object. Theactionis converted to Title Case for consistency.options- An optional set of extra arguments, which can include:label- An optional string to provide additional dimensions to the event data.value- An integer that you can use to provide numerical data about the user event.nonInteraction- A boolean that when set totrue, indicates that the event hit will not be used in bounce-rate calculation.NOTE: the data types of the optional properties on
optionsare important, and mismatches will cause values to be ignored.Also be aware that any string (e.g.,
actionorlabel) that look like an email address will cause the event to be redacted for privacy reasons (i.e., it will show up in your stats as “REDACTED (Potential Email Address)”;). An email address is loosely defined as any string of the form “..@..”.Each event will be tracked using the page’s hostname as the GA Category automatically.
Example 1: Using the old GA ga.js _gaq API
Example 2: Used as part of a Require.js module
Example 3: Using new GA Analytics.js ga() API
virtualPageview(virtualPagePath)
What are Virtual Pageviews?
Adapted From Google’s Documentation
The
virtualPageviewmethod is used to record custom GA virtualPageviews using either the old ga.js API, or the newer analytics.js API.It takes one argument:
virtualPagePath- A required string that is unique to the virtual pageview you want to trackNOTE: the ‘virtualPagePath’ value will be prefixed in GA reports with
/virtual/to distinguish these custom tracked ‘hits’ and to avoid clashes with existing (or potential page URLs).Usage is the same as the event() method documented above.
conversionGoal(action, options)
The
conversionGoalmethod is used to record Optimizely [https://help.optimizely.com/hc/en-us/articles/200039925](Custom Event Goals) and [https://help.optimizely.com/hc/en-us/articles/200039865](Revenue Tracking).It takes two arguments:
action- A required string that is used in the Optimizely admin interface during experiment setup. This string must match exactly to count as a conversion in a given experiment. Unlike GA events, the Optimizelyactionis not converted to Title Case. Avoid using spaces.options- An optional set of extra arguments:valueInCents- An optional integer that can be used to track revenue in A/B and multivariate testing experimentsNOTE: the data types of the optional properties on
optionsare important, and mismatches will cause values to be ignored.Include
webmaker-analticsin your page as shown in the GA examples above. Either directly, or as part of a Require.js module.Example: Non-financial conversion
Example: Financial conversion
Tests
To run the tests, do the following:
You can also run the tests in a browser by browsing to
test/index.html.