Showing posts with label Tricks. Show all posts
Showing posts with label Tricks. Show all posts

Monday, February 26, 2018

Code Your Way Corner How to implement Cross-Site In-Session Sitecore Analytics

I came across this weird scenario wherein we were migrating old legacy project coded in Vb.net into Sitecore. So during this process, we had to migrate in a prioritized manner which made difficult to share analytics across two different projects. So I have just noted down what I did in order to accomplish the same.

Assume we have a scenario with two different projects which are utilized for same Website (Some pages in Sitecore and Some pages in any other framework ). Let's Assume Sitecore Project named as "CodeYourWaySitecore" and other project named as  "CodeYourWayLegacy"
For CodeYourWaySitecore and CodeYourWayLegacy (“CodeYourWayLegacy.demo.com” and “CodeYourWayLegacy.demo.com”)  are hosted on different websites.  
By default in session, analytics work for each individual site.  Identifying and merging contacts at the end of the session allows for post-session interactions(assuming the contacts have been identified) but does nothing to address the user interactions on both sites.  Out of the box, no personalization on the CodeYourWaySitecore would kick in based on the interaction triggered on the CodeYourWayLegacy until after session end.
It turns out that you can share an analytics session assuming that the sites in question share a domain and you have a shared session database:
  1. Configure shared session cookies based on a domain by adding “ <httpCookies domain=”.demo.com”/>” (with your domain name of choice of course) in the system.web section of the web.config of both projects.
  2. Configure a shared session state database (instructions for 8.2 are here (Please note that while you use session DB it should be Sitecore session DB, For example: If your Sitecore instance is CodeYourWaySitecore then it would be CodeYourWaySitecore_Sessions)


If you want to add some diagnostics to view this real time, you can access the Tracker.Current.Session.Interaction data.  You will see it contains the same ContactId and InteractionId for both websites and the pages collection including page visits and goals triggered are in sync across both sites as well.
Voila – Analytics/ Interactions now works for both the sites exactly the same as it normally does for a single site.

Let me know if you face any difficulty in setting up.  


via MJ-Space

Other Sites :
Code Your Way | Travel Guide

Tuesday, August 8, 2017

Code Your Way Corner Disable Edit and Preview Modes on your CD servers

A genuine brisk post on something that confused me and made them scratch my head a week ago. A partner said that when perusing the production site with ?sc_mode=edit attached to the URL then the site would endeavor to divert the client to /Sitecore/login page. Sometimes, it would cause an unending sidetrack and make the program toss a "diverted too often mistake".

All by itself, it didn't cause any security issues – the CM server was just open to our interior system and we had taken after server solidifying best practices and blocked access to the CMS interface on the Compact disc servers (we in reality simply restore a 404 as opposed to anything particular identified with getting to denied).

We had additionally effectively followed manual for designing a CD server so was almost certain it was not identified with having missed debilitating a file.

So I inquired as to whether it was normal conduct that adding ?sc_mode=edit would endeavor to divert to the login page on the CD servers, I would have anticipated that it would do nothing. Having then delved a little into the Setting code turns out that there is a setting for this which we had totally ignored.

Include the accompanying patch your CD servers (including an entry for each site if they don't utilize the inherits attribute):



 <configuration xmlns:patch="http://ift.tt/1lHcKTh" xmlns:set="http://ift.tt/2mgHMLB">  
<sitecore>
<sites>
<site name="MJProdWebSite"
set:enablePreview="false"
set:enableWebEdit="false"
set:enableDebugger="false"
set:allowDebug="false"
/>
</sites>
</sitecore>
</configuration>

The notes in sitecore.config states the following:

enableDebugger: Shows if the debugger is enabled on the site. Ordinarily, this is just the website. 

enablePreview: Shows if the preview is enabled on the site. Normally this is just the website. 

enableWebEdit: Shows if WebEdit is enabled on the web page. Normally this is just the website. 


allowDebug: Must be true if you want to troubleshoot the website.


So this impairs edit, preview and troubleshoot modes for your site, making it totally disregard those URL parameters.

Straightforward as that. I checked a couple of Websites I know to be running on Sitecore and they had a similar issue.

Query String ?sc_mode=edit
This is home page for site and before sending request to server with ?sc_mode=edit as query-string

After sending the Request parameters it redirects to Sitecore login as below.




Go check your production site as well, perhaps you have not handicapped this either, a simple one to disregard. It merits evaluating alternate attributes and crippling as vital while you are there.



via MJ-Space

Other Sites :
Code Your Way | Travel Guide

Tuesday, July 25, 2017

Code Your Way Corner How to Preview an Item in Sitecore CMS

The preview functionality within Sitecore can be a useful tool when viewing pages on your website regardless of published status.
Using preview allows you to:
  • View the past or future version of your website based on a specified preview date and publishing restrictions.
  • View a webpage in different language versions.
  • View how a web page appears on various devices.
A Note on Sitecore Preview:
Pages may not always appear correctly within Sitecore Preview. Keep in mind that previewing will display page content regardless of publish status. This encompasses all associated items, including media.

Preview From the Content Editor


If you’re in Sitecore’s Content Editor, you can open Preview by first selecting a web page item that you would like to preview and then click “Preview” within the Publish group on the Publish tab.


Sitecore Preview from Content Editor

A preview of the page item will open in a new tab.


Sitecore page Preview 

Alternatively, the preview option may be selected from the preview group in the Presentation tab to view a preview of the page within Content Editor.


Sitecore Page Preview from presentation


Preview from the Experience Editor

If you’re in Sitecore’s Experience Editor, Preview functionality may be accessed by selecting “Preview” from “Other” drop-down in the Mode group on the Experience tab in the ribbon.
The ribbon may be collapsed or expanded in Preview mode by clicking the Toggle the Ribbon icon in the upper right of the screen, beneath user.

A Note on Sitecore Permissions:
Please note that if your username does not have proper permissions, the Experience tab may not be accessible.
Sitecore Preview from Experience Editor


Setting Language Preview Parameter


If your website has more than one language version, the Language preview parameter may be set to preview how a web page may appear in a different language version. This may be done by clicking the current language within the Language group on the Versions tab in the ribbon.

Sitecore Preview Language

Available language options will be visible. Simply select a language from the list to view the page in that language version.

Sitecore  Preview Language Versions

Preview from the Device Simulator

To view how your website might appear on different devices, such as an iPhone, iPad, Android Phone, and Blackberry, preview it from the Device Simulator.
Although the simulated rendition of the previewed website may not be 100% accurate, the Device Simulator provides content authors the ability to quickly preview pages on mobile devices and it shows a good example of how a page might look on each device.
To access the Device Simulator, first, open “Preview” for the page being previewed. To select a device for simulation, click the “Simulator Button” in the Simulators group.

Sitecore Preview Device Simulator
A range of devices appear and a selection may be made for simulation. Make note of the “None” device option, as this is the selection that should be made for default page preview.

Sitecore  Preview Devices

Credits: Sitecore, GeekHive


via MJ-Space

Other Sites :
Code Your Way | Travel Guide

Monday, December 19, 2016

Code Your Way Corner Handle Divide-By-Zero Error In SQL

The idea here is that, as with any other form of math that we know of, we cannot divide by zero in a SQL call. Therefore, running this code :


 SELECT  ( 3765 / 0 ) AS value  ;  

if  you ever tried this , result of SQL server would be  :





To prevent this sort of error from being thrown, author Hugo Kornelis suggests using a NULLIF() in the divisor of the equation. NULLIF() takes two arguments and returns NULL if the two values are the same and can be used to turn the divisor from a zero into a NULL which, in turn, will force the entire equation to become NULL



  SELECT  ( 3765/ NULLIF( 0, 0 ) ) AS value  ;  

Therefore, while we try running modified query (as above) , we would end up getting Null Object , which reduces the risk of handling divide by zero :


NULLIF(0,0) returns NULL since zero is equal to zero , which makes SQL statement to return NULL . Till this point its pointless example since both zero values are hard coded .

But imagine if in case it was a user-centered value , or a SQL aggregate or any other calculated  value it would be of great help in handling error.

What if we want a default value when there is NULL ? It's just simple . Just use another inbuilt function like ISNULL() or COALESCE() . Query and Result would look like :


Explanation :
We are performing division in inner-query , but then if it returns NULL , then ISNULL() function would catch it and return Zero as default value . 

This would be handy for those who runs tons of reports on DB table.

Sources : Hugo_kornelis Blog


via MJ-Space

Other Sites :
Code Your Way | Travel Guide

Tuesday, October 4, 2016

Code Your Way Corner AngularJs -Effective way to Monitor Changes to Model data


The two-way data binding in AngularJS is very powerful, especially when there is a direct mapping of input data to view-model-data. But, when the data mapping requires some translation, interpretation, or validation, you need to get your hands a little dirty - you need to get more involved in how data changes are propagated within your Controller(s). As this has happened to me, I've been forced to think more deeply about my AngularJS application architecture; and, as a result, I've found that my life is often simplified (in the long term) by calling a directive to watch before I bind the data to View model.

Factory To Monitor Changes


This is easiest and effective way because of these reasons :

1) I just need to call a Trigger Method with scope which should be monitored
2) Once the data is retrieved from Ajax call , I just Initialize the data
3) If there are any changes to bound data on the form and user tries to navigate to another page, I get Confirm box on the page and hence easy to watch.
4) If there are updates from the server, I just Reset the data - which means you are saying to watch it on new data forgetting old data .

Below is factory which can be used across your application :

Just create a JS file with below code in it and include factory in your base js, for example, app.js

/* Factories */
 'CYW.aysfactory',


 'use strict';  
angular.module('CYW.aysfactory', [])
.factory('$CYWAreYouSure', ['$q', '$window',
function factories_ays($q, $window) {
/// <summary>
/// Factory for the ARE YOU SURE
/// </summary>
/// <param name="$q">The $q.</param>
/// <param name="$window">The $window.</param>
return {
Dirty: false,
Original: null,
Title: 'Are you sure?',
Message: 'Changes you made may not be saved. Do you wish to continue and lose your changes?',
SetTitle: function (title) {
/// <summary>
/// sets the title to 'Are you sure?'
/// </summary>
/// <param name="title">The title.</param>
this.Title = title;
},
SetMessage: function (msg) {
/// <summary>
/// Sets the message to msg
/// </summary>
/// <param name="msg">The MSG.</param>
this.Message = msg;
},
Initialize: function ($scope, dataToWatch) {
/// <summary>
/// Initialize tje data to watch in the scope
/// </summary>
/// <param name="$scope">The $scope.</param>
/// <param name="dataToWatch">The data to watch.</param>
var that = this;
$scope.$watch(function () {
return dataToWatch
}, function (newVal, oldVal) {
/// <summary>
/// sets the data to watch
/// </summary>
/// <param name="newVal">The new value.</param>
/// <param name="oldVal">The old value.</param>
if (newVal) {
if (!that.Original) {
that.Original = JSON.stringify(newVal);
}
else {
if (JSON.stringify(newVal) != that.Original) {
that.Dirty = true;
}
}
}
}, true);
$window.onbeforeunload = function () {
/// <summary>
/// returns dirty message
/// </summary>
if (that.Dirty) {
return that.Message
}
else {
return;
}
};
},
Reset: function (updatedData) {
/// <summary>
/// Reset the dirty message
/// </summary>
/// <param name="updatedData">The updated data.</param>
this.Dirty = false;
this.Original = null;
},
Trigger: function ($scope) {
/// <summary>
/// triggers dirt message for ARE YOU SURE message
/// </summary>
/// <param name="$scope">The $scope.</param>
var def = $q.defer();
if (this.Dirty) {
confirm($scope, this.Title, this.Message).then(function (result) {
if (result) {
def.resolve();
}
else {
def.reject();
}
}, function () {
def.reject();
});
}
else {
def.resolve();
}
return def.promise;
}
};
}
])
;



Now you have code in place its time to use it  : 

I have a function which does an HTTP call to get dummy data . We just wrap it in trigger function and Initialize once data is back from the server as shown in Red.


 var getDataFromServerAndBind = function () {  
var def = $q.defer();
$CYWAreYouSure.Trigger($scope).then(function () {
/// <summary>
/// AYS message based on scope retrieval
/// </summary>
$cywHttp.get('getSomeDummyData/').then(function (data) {
$scope.data = data;
$scope.bindGrid();
$CYWAreYouSure.Initialize($scope, $scope.data);
def.resolve();
}, function () {
def.reject();
});
}, function () {
def.reject();
});
return def.promise;
}

So now we have data imagine person changes data and you have some other API to update model ,so we need to call Reset  function and bind it back as above .



  $scope.AddOrUpdateDummyData = function () {  
return $cywHttp.put('putSomeDummyData/putEmployeeRoles', {
data: employeeRolesListingModel
}).then(function (data) {
$cywAreYouSure.Reset($scope.data);
// Just Rebind the Grid inorder to show new add/update of employee role which we created before
getDataFromServerAndBind();
});
};

That's it !!! . Now Whenever a user changes values /there is a change in values of the model and there are no saved changes Factory automatically pops up a confirm box to show user it's not saved . 

You can even set the Title and message separately for each page you are using.

Thanks for Visiting .

Do share your views . For more updates just follow the Fb page


via MJ-Space

Other Sites :
Code Your Way | Travel Guide