默认的Cloud Services集可以使用自定义Cloud Service类型进行扩展。 这允许您以结构化方式将自定义标记注入页面。 这将主要用于第三方分析提供商,例如Google Analytics、Chartbeat等。 Cloud Services从父页面继承到子页面,能够在任何级别中断继承。
此创建新Cloud Service的分步指南是使用Google Analytics的示例。 所有内容可能不适用于您的用例。
在CRXDE Lite中,在/apps
下创建新节点:
acs
nt:folder
在/apps/acs
下创建新节点:
analytics
sling:Folder
在/apps/acs/analytics
下创建2个新节点:
sling:Folder
和
sling:Folder
右键单击/apps/acs/analytics/components
。 选择创建……后跟创建组件……打开的对话框允许您指定:
googleanalyticspage
Google Analytics Page
cq/cloudserviceconfigs/components/configpage
.hidden
单击Next两次,然后指定:
acs/analytics/templates/googleanalytics
单击Next两次,然后单击OK。
将属性添加到googleanalyticspage
:
cq:defaultView
html
在/apps/acs/analytics/components/googleanalyticspage
下创建一个名为content.jsp
的新文件,其中包含以下内容:
<%@page contentType="text/html"
pageEncoding="utf-8"%><%
%><%@include file="/libs/foundation/global.jsp"%><div>
<div>
<h3>Google Analytics Settings</h3>
<ul>
<li><div class="li-bullet"><strong>accountID: </strong><br><%= xssAPI.encodeForHTML(properties.get("accountID", "")) %></div></li>
</ul>
</div>
在/apps/acs/analytics/components/googleanalyticspage/
下创建新节点:
名称: dialog
类型: cq:Dialog
属性:
title
String
Google Analytics Config
xtype
String
dialog
在/apps/acs/analytics/components/googleanalyticspage/dialog
下创建新节点:
名称: items
类型: cq:Widget
属性:
xtype
String
tabpanel
在/apps/acs/analytics/components/googleanalyticspage/dialog/items
下创建新节点:
items
cq:WidgetCollection
在/apps/acs/analytics/components/googleanalyticspage/dialog/items/items
下创建新节点:
名称:tab1
类型: cq:Panel
属性:
title
String
Config
在/apps/acs/analytics/components/googleanalyticspage/dialog/items/items/tab1
下创建新节点:
名称:项目
类型: nt:unstructured
属性:
名称: fieldLabel
类型:字符串
值:帐户ID
名称: fieldDescription
类型: String
值: The account ID assigned by Google. Usually in the form UA-NNNNNN-N
名称: name
类型: String
值: ./accountID
名称: validateOnBlur
类型: String
值: true
名称: xtype
类型: String
值: textfield
将/libs/cq/cloudserviceconfigs/components/configpage/body.jsp
复制到/apps/acs/analytics/components/googleanalyticspage/body.jsp
,将第34行的libs
更改为apps
,并将第79行的脚本引用设为完全限定的路径。
在/apps/acs/analytics/templates/
下创建新模板:
acs/analytics/components/googleanalyticspage
googleanalytics
Google Analytics Configuration
/etc/cloudservices/googleanalytics(/.*)?
/apps/acs/analytics/templates/googleanalytics
cq/cloudserviceconfigs/templates/configpage
(在模板节点上,而不是jcr:content节点)/etc/designs/cloudservices/googleanalytics
(在jcr:content上)创建新组件:/apps/acs/analytics/components/googleanalytics
。
将以下内容添加到googleanalytics.jsp
:
<%@page import="org.apache.sling.api.resource.Resource,
org.apache.sling.api.resource.ValueMap,
org.apache.sling.api.resource.ResourceUtil,
com.day.cq.wcm.webservicesupport.Configuration,
com.day.cq.wcm.webservicesupport.ConfigurationManager" %>
<%@include file="/libs/foundation/global.jsp" %><%
String[] services = pageProperties.getInherited("cq:cloudserviceconfigs", new String[]{});
ConfigurationManager cfgMgr = resource.getResourceResolver().adaptTo(ConfigurationManager.class);
if(cfgMgr != null) {
String accountID = null;
Configuration cfg = cfgMgr.getConfiguration("googleanalytics", services);
if(cfg != null) {
accountID = cfg.get("accountID", null);
}
if(accountID != null) {
%>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '<%= accountID %>']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script><%
}
}
%>
这应该根据配置属性输出自定义标记。
导航到http://localhost:4502/miscadmin#/etc/cloudservices
并创建新页面:
Google Analytics
googleanalytics
返回CRXDE Lite,在/etc/cloudservices/googleanalytics
下,将以下属性添加到jcr:content
:
componentReference
String
acs/analytics/components/googleanalytics
导航到新创建的服务页面(http://localhost:4502/etc/cloudservices/googleanalytics.html
),然后单击+以创建新配置:
/etc/cloudservices/googleanalytics
My First GA Config
选择Google Analytics配置并单击创建。
输入帐户ID,例如AA-11111111-1
。 单击确定。
导航到某个页面,并在Cloud Services选项卡下的页面属性中添加新创建的配置。
页面将在其中添加自定义标记。