Sunday, 23 November 2014

Introduction to Sling Servlets in AEM

Hi All

This blog intended to the beginners of CQ5 to understand what are the sling servlet and how to create a very basic servlet. We can create the servlet in AEM by two ways :

1) By using Sling-specific @SlingServlet annotation.
2) By using  generic maven-scr-plugin annotations.



Here Below i'll show you an example how to resolve the servlet as per the resourceTypes.

Step 1 : Create a component /apps/AEMProject/components/content/servletchecker.
Step 2 : Inside servletchecker.jsp Add the below code.

 <form name="form" method="post" action="<c:out value="${resource.path}" />">   
   <input type="submit" />   
  </form>   

Here resource.path refer to the content node, that might all aware :)
Step 3 : Create the servlet as follows :
 @Component   
  @Service(Servlet.class)   
  @Properties({   
    @Property(name = "sling.servlet.resourceTypes" , value = "AEMProject/components/content/servletchecker" ),   
    @Property(name = "sling.servlet.methods" , value = "POST" )   
  })   
  public class ConfigurationChecker extends SlingAllMethodsServlet {   
    private static final long serialVersionUID = -7945683216509362012L;   
    private final static Logger logger = LoggerFactory.getLogger(ConfigurationChecker.class);   
     @Override   
     protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {   
           response.setContentType("text/plain");   
           logger.info("Success");   
     }   
  }   

Here as you can see action is resource.path, in my case it is /content/AEMProject/English/test/jcr:content/par/servletchecker
At this node Sling will look for AEMProject/components/content/servletchecker resource as we registered in our servlet, If it gets resolve then the servlet execute.

Also one can use the selectors to resolve in that case your jsp would be like :
 <form name="frm" method="post" action="<c:out value="${resource.path}" />.email">  
   <input type="submit" />  
 </form>  


And add the property on your servlet :
@Property(name = "sling.servlet.selectors" , value = "email")

Below would be the code :


 @Override  
        protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {  
                   response.setContentType("text/plain");  
                   String selectors[] = request.getRequestPathInfo().getSelectors();  
                   logger.info("Resource is "+resourceType);  
                   for(int i=0 ; i<selectors.length ; i++)  
                   {  
                        String selector = selectors[i];  
                        if(selector.equals("email"))  
                        {  
                             logger.info("Work with Email");  
                        }  
                   }  
                   logger.info("Success");  
        }  

Thanks



Wednesday, 12 November 2014

AEM integration with Google Analytics and Google Tag Manager




Google Analytics and Google Tag Manager are analytical tools that help to analyze the statistics of web site which is helpful as per the business prospective. Organization can monitor which are widely used web pages and which are least use by the visitor. Here I am going to demonstrate how you can integrate AEM with Google Analytics and also will use the GTM tags to use in AEM.


1)      Organization must have the google analytics account, If one already have then we can get the tracking code by navigate to the admin tab then click on the tracking info section and copy your tracking id and tracking code on some txt file as these two properties initially we need.
2)      Go to tools section of AEM. Navigate to the Generic Analytics Snippet and create the new configuration. Inside Snippet JavaScript Code just paste your tracking code & ok.
 
1)      We need to add this cloudservice onto the page that we want that we want to analyze.  If we want to analyze the whole site then just add that cloud service onto the topmost page of hierarchy like en-US.
2)      Now login to the Google Tag Manager. Follow the link to create a tag that will track outbound link. Below is the snapshot of rule that I applied. Also make sure you will have the js code that you get from GTM  inside body.jsp or what they mentioned in blog.




1)      For testing you can also debug your tag via overview then upon clicking preview button.
2)      Open you site hosted on 127.0.0.1:8502 then open some page and click on any external link.  Now any click event that redirect you out of the site will get track.
3)      We can check the analytics. Go to the reporting tab and in search look for the event you will find Behavior >Event >Page the you will be able to see events. Click on the page on which event fired.