Skip to content

Commit

Permalink
Added automatic text selection event trigger. Added support for regis…
Browse files Browse the repository at this point in the history
…ter custom dimensions
  • Loading branch information
fabiobuda committed Feb 4, 2017
1 parent 0ecb984 commit 387775e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 7 deletions.
102 changes: 95 additions & 7 deletions EventAnalytics-1.0.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Event Analytics 1.0
Event Analytics 1.0.1
The developer friendly javascript library to do
Event Tracking using Google Analytics or Piwik
Expand Down Expand Up @@ -31,6 +31,13 @@
-- END GNU GPL LICENSE
*/

/*
ChangeLog:
v1.0.1 - 04.02.2017 fabiobuda@netd.it
[Feature] Added support for get selected text on Desktop and Mobile
[Feature] Added support to register custom dimensions (simple proxy to Piwik or GA)
*/

/*
Startswith Polyfill
fabiobuda@netd.it - 25-10-2015
Expand Down Expand Up @@ -69,6 +76,7 @@ EventAnalytics._Init = function(){
var Scroll = {"25":false, "50":false, "75":false};
EventAnalytics.Scroll = Scroll;
EventAnalytics.InitScrollEvents();
EventAnalytics.EventAnalytics.InitTextSelection();
}

EventAnalytics.RegisterNewVisitorCallback = function( callback, delay ){
Expand Down Expand Up @@ -132,6 +140,14 @@ $( window ).scroll(function() {
});
}

EventAnalytics.InitVisitor = function(){
if( EventAnalytics.IsGA ){
EventAnalytics.InitVisitorGA();
} else if( EventAnalytics.IsPiwik ){
EventAnalytics.InitVisitorPiwik();
}
}

EventAnalytics.InitVisitorPiwik = function(){
var VisitorInfo = EventAnalytics.Tracker.getVisitorInfo();
// Is new visitor seems to not work :(
Expand Down Expand Up @@ -176,12 +192,11 @@ EventAnalytics.InitVisitorGA = function(){
}
}

EventAnalytics.InitVisitor = function(){
if( EventAnalytics.IsGA ){
EventAnalytics.InitVisitorGA();
} else if( EventAnalytics.IsPiwik ){
EventAnalytics.InitVisitorPiwik();
}
EventAnalytics.InitTextSelection = function(){
EventAnalytics.Utils.StartListenSelectTextEvent( EventAnalytics._TextSelectCallBack );
}
EventAnalytics._TextSelectCallBack = function( text ){
EventAnalytics.RegisterUIEvent("document", "selection", "text", text);
}

EventAnalytics.RegisterUIEvent = function( category, action, name, value ){
Expand All @@ -194,6 +209,28 @@ EventAnalytics.RegisterUIEvent = function( category, action, name, value ){
}
}

/*
Custom Dimensions is a feature that have both Piwik and GA
* Piwik limits the number of custom dimensions to 5 even if you can
configure more custom dimensions rebuilding your DB schema
Many info at:
https://piwik.org/docs/custom-dimensions/#data-limits-for-custom-dimensions
http://piwik.org/faq/how-to/faq_17931/
* GA limits the number of custom dimensions to 20 for free users
and to 200 for premium Google Analytics Users
Many info at:
https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets
We leave to the user the correct management of dimensions
*/
EventAnalytics.RegisterCustomDimension = function( index, value ){
if( EventAnalytics.IsGA ){
ga('send', 'pageview', { index: value });
} else if( EventAnalytics.IsPiwik ){
EventAnalytics.Tracker.setCustomDimension(index, value);
}
}

EventAnalytics.ParseNameVariables = function( event, varname ){
var element = $(event.target);
var content = "unkwown";
Expand Down Expand Up @@ -359,3 +396,54 @@ EventAnalytics.CookieStorage.read = function(nameEQ){
EventAnalytics.CookieStorage.erase = function(name){
EventAnalytics.CookieStorage.create(name,"",-1);
}


/*
Utils contains useful functions for
getting selected text by user
*/
EventAnalytics.Utils = EventAnalytics.Utils || {};

EventAnalytics.Utils.getSelectedText = function(){
var ret = '';
if (window.getSelection) {
ret = window.getSelection().toString();
} else if (document.selection) {
ret = document.selection.createRange().text;
}
return ret;
}

EventAnalytics.Utils.LastSelectedText = false;
EventAnalytics.Utils.SelectTextEventFired = false;

/*
Tested on:
Chrome, Firefox, Opera Desktop
iOS 10 Safari, Android 4.4.2 Chrome
It works very well on Desktop and iOS
but is not stable on Android Chrome
*/
EventAnalytics.Utils.StartListenSelectTextEvent = function( callback ){
$(document).on("mouseup touchend onselectstart onselectend onselectionchange", function(e) {
e.preventDefault();
if( !EventAnalytics.Utils.SelectTextEventFired ){
var text=EventAnalytics.Utils.getSelectedText();
if (text!='' && text != EventAnalytics.Utils.LastSelectedText){
EventAnalytics.Utils.LastSelectedText = text;
callback( text );
}
EventAnalytics.Utils.SelectTextEventFired = true;
setTimeout(function(){ EventAnalytics.Utils.SelectTextEventFired = false; }, 50);
}
});
}

EventAnalytics.Utils.getMetadata = function(){
var title = document.title;
var description = $("meta[name='description']").attr("content");
var keywords = $("meta[name='keywords']").attr("content");
//alert(title);
//alert(description);
//alert(keywords);
}
Binary file modified docs/EventAnalyticsUML.dia
Binary file not shown.

0 comments on commit 387775e

Please sign in to comment.