Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Accessing SlingServlet Data into SlingModel

Avatar

Level 3

Hi All,

I have created Search Servlet  and as per the requirement I need to track search data like search count ,search result.

so for that I have created Sling Model for Analytics Tracking of current page data and For that Sling Model I need search data as well.

does anyone worked on same type of requirement.

 

1 Accepted Solution

Avatar

Correct answer by
Adobe Champion

If you are running server-side code to push this information over to Analytics or some other back end system, then I would agree with Brian's post here.  Sling models are generally for representing a JCR resource that correlates with a page component.  An OSGi service is going to provide you access to all of the same contexts that would be applicable to your task, and would be easier to leverage from your search servlet.

 

However, if you're looking for Analytics to pick this data up from the front end web page (similar to other Analytics data), it would be great to simply push this context into the Analytics datalayer.  Rather than using a servlet for search results, you can create a Sling Model for your "search_results" component - that sling model could A) execute the search and then B) update the datalayer with the data to by picked up by Analytics.

 

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @savi50,

 

You can inject servlets like this

 

@Inject private List<Servlet> servlets;

 

Thanks,

Kiran Vedantam.

Avatar

Community Advisor

As a best practice to enforce re-usability, Maybe you can extract the business logic from the servlet into an OSGI service, so that all of your OSGI components can be using the same service. 

Avatar

Correct answer by
Adobe Champion

If you are running server-side code to push this information over to Analytics or some other back end system, then I would agree with Brian's post here.  Sling models are generally for representing a JCR resource that correlates with a page component.  An OSGi service is going to provide you access to all of the same contexts that would be applicable to your task, and would be easier to leverage from your search servlet.

 

However, if you're looking for Analytics to pick this data up from the front end web page (similar to other Analytics data), it would be great to simply push this context into the Analytics datalayer.  Rather than using a servlet for search results, you can create a Sling Model for your "search_results" component - that sling model could A) execute the search and then B) update the datalayer with the data to by picked up by Analytics.