Client Tracker

  0 rating
4/9/2014 8:28:52 AM
4/17/2015 1:40:38 PM
Source: VisionsInCode
Provider: VisionsInCode

About

This module allows you to track events/goals and also visitors current geolocation.
If you have questions, improvements, finding bugs - Please let me know.

Documentation

Documentation
  • Documentation > Client Tracker
    Client Tracker


    Today when more and more users/visitors are using handheld devices with GPS for browsing the internet, I wanted to get the actual location for the visitor using HTML5 Geolocation API.

     

    This is an extension of ALEXANDER DOROSHENKO'S CLIENT EVENT TRACKER, http://marketplace.sitecore.net/en/Modules/Client_Event_Tracker.aspx

     

    I did some minor changes. Instead of passing a number of parameters to the javascript method, AnalyticsPageEvent, I changed it to take a Json object.


    The json object is a dictionary which supports following:
    Coordinates (Track current user/visitor location)
    PageEventId    (Track event/goal)
    PageUrl        (The url/page where the event/goal is fired)
        

    Installation guide:

    1. Install the package(Contains only files):
    /TrackerHandler.ashx
    /ClientEventTracker.js
    /PageScriptSample.js
    /bin/Sitecore.SharedSource.ClientTracker.dll

    2. The package will put TrackerHandler.ashx, ClientEventTracker.js and PageScriptSample.js in the root. I strongly suggest/recommend that you move the files to proper folders. PageScriptSample.js is just a sample.
        
    3. Add the following code to the page where you want to do client tracking:
    <script src="your path/ClientEventTracker.js" type="text/javascript"></script>

    4. How to use it, please take a look at PageScriptSample.js.
    Check also following blog posts:
    http://visionsincode.wordpress.com/2014/03/24/client-side-tracking-with-sitecore-dms
    http://visionsincode.wordpress.com/2014/04/02/track-a-visitors-coordinates-in-the-sitecore-dms

    Note. Sitecore.SharedSource.ClientTracker.dll is using Newtonsoft.Json.dll


    0
Back
Release notes
The module was released 4/8/2014
If you have questions, improvements, finding bugs - Please let me know.
Read more Back
Code examples
  • Code examples > Example code
    Sample code for geolocating visitors
    var SharedSource = SharedSource || {};

    jQuery(document).ready(function () {
        SharedSource.ClientTracker.DomReady();
    });

    SharedSource.ClientTracker = {
        DomReady: function() {

            SharedSource.ClientTracker.Init(true);

            //Some example event
            jQuery("xxxxxxx").click(function () {

                SharedSource.ClientTracker.TrackPageEvent();

            });

        },

        Init: function(trackUser) {

            window.pathToHandler = "/TrackerHandler.ashx";

            if (navigator)
                navigator.geolocation.getCurrentPosition(geoSuccess, geoError);

            function geoSuccess(p) {

                window.coordinates = p.coords;

                if (trackUser) {
                    SharedSource.ClientTracker.TrackUserLocation();
                }

            }

            function geoError(error) {
                var message = "";
                switch (error.code) {
                case error.PERMISSION_DENIED:
                    message = "This website does not have permission to use " + "the Geolocation API";
                    break;
                case error.POSITION_UNAVAILABLE:
                    message = "The current position could not be determined.";
                    break;
                case error.PERMISSION_DENIED_TIMEOUT:
                    message = "The current position could not be determined " + "within the specified timeout period.";
                    break;
                }

                if (message == "") {
                    var strErrorCode = error.code.toString();
                    message = "The position could not be determined due to " + "an unknown error (Code: " + strErrorCode + ").";
                }

                Logging(message);
            };


        },
        TrackUserLocation: function() {

            if (!window.coordinates)
                return;

            var requestParamAndValues = {};
            requestParamAndValues["Coordinates"] = SharedSource.ClientTracker.StringFormat("{0},{1}", window.coordinates.latitude, window.coordinates.longitude, '1');

            var jsonObject = {};
            jsonObject["requestParamAndValues"] = requestParamAndValues;

            var analyticsEvent = new AnalyticsPageEvent(jsonObject, window.pathToHandler);
            analyticsEvent.trigger();

        },
        StringFormat: function () {
            var s = arguments[0];
            for (var i = 0; i < arguments.length - 1; i++) {
                var reg = new RegExp("\\{" + i + "\\}", "gm");
                s = s.replace(reg, arguments[i + 1]);
            }
            return s;
        },

        Logging: function (message) {
            if (typeof console == "object") {
                console.log(message);
            }
        },

        TrackPageEvent: function() {

            var dataContainer = jQuery("xxxxxx");

            var requestParamAndValues = {};
            requestParamAndValues["PageEventId"] = dataContainer.data("goal");
            requestParamAndValues["PageUrl"] = dataContainer.data("pageurl");

            if (window.coordinates)
                requestParamAndValues["Coordinates"] = SharedSource.ClientTracker.StringFormat("{0},{1}", window.coordinates.latitude, window.coordinates.longitude, '1');


            var jsonObject = {};
            jsonObject["requestParamAndValues"] = requestParamAndValues;

            var analyticsEvent = new AnalyticsPageEvent(jsonObject,  window.pathToHandler);
            analyticsEvent.trigger();
        }

        

        
    };
    0

Solution screenshots(0)

Upload

Reviews (0)

Sort by: Date Most votes
  • Profile Avatar
    [fullName]

    Level: 0

    x0 x0 x0

    [date]

    [title]

    [text]

    Was this helpful?

    0

Comments (0)

Sort by: Date  Most votes

Leave a Comment

Comment must be field in
Post comment
loader

Write a review

Title can't be empty
Review can't be empty
Post review

Download

Title Description Download Action

Add File