Monday, March 27, 2017

Show Page View Count based on location

Create List Structure:

List Title : "PageAnalytics"

Columns Type

Title(Country) (Single Line of Text)

PageTitle (Single Line of Text)

UserID (Number)

Script to Save Current User Location on Page View and Also Fetch Page View Details

});//Fetching Current Users Location from site 'http://freegeoip.net'
$.getJSON("http://freegeoip.net/json/", function(data) {
var countryName = data.country_name;
//In case any other details need to be saved or displayed other than country name
/*
var country_code = data.country_code;
var ip = data.ip;
var time_zone = data.time_zone;
var latitude = data.latitude;
var longitude = data.longitude;
*/
storeCurrentUser(countryName);
getPageViews();
});
//Adding Page View Detail of current user to List
function StoreCurrentUser(countryName){
var currentPage=$(document).attr('title');
var currentUser=_spPageContextInfo.userId;
var data = {
    __metadata: { 'type': 'SP.Data.PageAnalyticsListItem' },
    Title: countryName,
    PageTitle: currentPage,
    UserID: currentUser
};
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('PageAnalytics')/Items",
    type: "POST",
    headers: {
        "accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),
        "content-Type": "application/json;odata=verbose"
    },
    data: JSON.stringify(data),
    success: function (data) {
        console.log(data);
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
}
//Fetching current Page view from list filtered by Page Title
function getPageViews(){
var currentPage=$(document).attr('title');
var countryArray=[];
var userArray=[];
var viewCount={};
$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('PageAnalytics')/Items?$orderby=Title&$filter=PageTitle eq " + currentPage,
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    success: function (data) {        
        console.log(data.d.results);
        $.each(data.d.results,function(i,val){
            //for unique Page Unique User Count                
            if(userArray.indexOf(val["UserID"]) < -1 && countryArray.indexOf(val["Title"]) < -1){
                userArray.push(val["UserID"]);
                countryArray.push(val["Title"]);
                viewCount[val["Title"]]={
                    Country : val["Title"],
                    Count : 1;
                }
            }
            else{
                viewCount[val["Title"]]["Count"]=viewCount[val["Title"]]["Count"]+1;
            }
            //Unique page and Non Unique Users
            /*if(userArray.indexOf(val["UserID"]) < -1 && countryArray.indexOf(val["Title"]) < -1){
                userArray.push(val["UserID"]);
                countryArray.push(val["Title"]);
                viewCount[val["Title"]]={
                    Country : val["Title"],
                    Count : 1;
                }
            }
            else{
                viewCount[val["Title"]]["Count"]=viewCount[val["Title"]]["Count"]+1;
            } */
        });
        //final Page wise count can be found here
        console.log(viewCount);
    },
    error: function (error) {
        alert(JSON.stringify(error));
    }
});
}

No comments:

Post a Comment