Expand my Community achievements bar.

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

Code snippet inside head tag with dynamic value

Avatar

Level 1

Hi All,

I want to include a code snippet inside the head tag in head.jsp for a third party integration.

But the code snippet has a unique code for each of the environments. How can I implement this?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

3 Replies

Avatar

Community Advisor

can't you include in page component customheaderlibs? 

you can use sling model to pass environment specific code to head.jsp

 

@Model(adaptables = Resource.class)
public class EnvironmentSpecificCodeModel {

@ValueMapValue
private String environmentSpecificCode;

@Inject
public EnvironmentSpecificCodeModel(Resource resource) {
// Fetch the environment-specific code here based on your needs
}

public String getEnvironmentSpecificCode() {
return environmentSpecificCode;
}
}

 

<%
// Use the Sling Model to fetch the environment-specific code
com.yourproject.core.models.EnvironmentSpecificCodeModel codeModel = resource.adaptTo(com.yourproject.core.models.EnvironmentSpecificCodeModel.class);
String environmentSpecificCode = codeModel.getEnvironmentSpecificCode();
%>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
<%=environmentSpecificCode%>
</script>
</head>
<body>
<!-- Your body content here -->
</body>
</html>

Avatar

Level 4

Hi @RahulVo

In the Core Component Page v3 there is an opportunity to load developer-defined context-aware CSS, Javascript or meta tags. This is done by creating a context-aware resource for com.adobe.cq.wcm.core.components.config.HtmlPageItemsConfig.

See more details: https://github.com/adobe/aem-core-wcm-components/tree/main/content/src/content/jcr_root/apps/core/wc... 

 

However, it doesn't allow uploading code snippet. You can reuse their approach with CA-configuration, because it will allow you to configure and modify this code snippet in runtime on AEMaaCS. 

Avatar

Level 4

We used CA configs and sling models to achieve the same.