SharePoint 2013: Log Analytics through javascript


The following code snippet will help you to log the usage analytics into ULS:

//Log Views usage event on URL of catalog item
    window.LogViewsToEventStore = function(url, site)
    {    
        SP.SOD.executeFunc(“sp.js”, “SP.ClientContext”, function()
        {
            var spClientContext = SP.ClientContext.get_current();
            if(!$isNull(spClientContext))
            {
                    var spWeb = spClientContext.get_web();
                    var spUser = spWeb.get_currentUser();
                    var spScope = “{00000000-0000-0000-0000-000000000000}”;
                    
                    //Event ID = 1 (Views), 2 (
                    SP.Analytics.AnalyticsUsageEntry.logAnalyticsEvent2(spClientContext, 1, url, spScope, site, spUser);spClientContext.executeQueryAsync(null, null);                   
            }
        });
    };
    
    LogViewsToEventStore(‘Url’, ‘SiteID’);

You can use this in your CSWP or other webparts on client side, with ease.

One thought on “SharePoint 2013: Log Analytics through javascript

Feel free to leave a reply here...

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s