0

I want to get the type of the page i.e PLP or PDP page

Nikhil Patil
  • 11
  • 3
  • 8

1 Answers1

0

You can determine the "type" of the page in a couple different ways within SFCC's controllers or backend logic (Server-exectued JavaScript) as well as some other ways when trying to detect this condition in frontend logic. (Browser-executed JavaScript) Additionally, for the frontend and backend solutions it may be different depending on the implementation that you're working on. Depending on it's age, and who implemented it, you may be working in a SiteGenesis, MFRA, or SFRA context. Possibly, even a 100% custom implementation. Therefore please accept this as a very general answer given that the question currently has absolutely no context whatsoever.

Backend Context

In all backend contexts, the solution relies upon the "ClickPath" data store of a session. You must be conscious that this may not be 100% accurate as there could be race conditions wherein users browsing in multiple tabs may have pages visited after the current request is processed and it is unknown whether the last entry in the ClickStream could be 'newer' than the request being processed currently.

Why the pipelineName attribute isn't present on the Request class is a bit confusing to me. Why wouldn't it be? Regardless, we must do what we must do.

SFRA

// in the context of a Controller method
req.session.clickStream.last.pipelineName // returns string like: 'Pipeline-Name'

All other contexts (Site Genesis & MFRA)

// session is a global variable in SFCC. It should be accessible 
// in all backend contexts with the exception of certain hook 
// contexts.
session.clickStream.last.pipelineName // returns string like: 'Pipeline-Name'

Frontend Context

The frontend context which represents JavaScript running in the browser relies upon variables that have been set by the server within the browser's execution context. Specifically, variables that are accessible within the context of the app.js monolithic module in the case of SiteGenesis or SFRA 'client' modules

SFRA

In SFRA, there's a kind of 'global' JS module which is what you find in main.js and then there are type-specific JS modules like productDetail.js and search.js. In the context of SFRA I'd recommend add your modules/scripts to one of those page-type-specific modules in order to execute different logic based on the page type.

Site Genesis

Within the context of JavaScript executing on the page the following expression will typically give sufficient values to adjust behavior based on page type. Note, you may need to have this expression execute in the context of the closure within app.js in order for it to work.

app.page.type // equals 'product' or 'search' for example (IIRC)
sholsinger
  • 3,028
  • 2
  • 23
  • 40