カスタムクラウドサービスの作成 creating-a-custom-cloud-service
デフォルトの Cloud Services を、カスタムクラウドサービスタイプで拡張することができます。これにより、カスタムマークアップを構造的な方法でページに挿入できます。この手法は主に、Google Analytics や Chartbeat などのサードパーティの分析プロバイダーで使用されます。Cloud Services は、親ページから子ページに継承されますが、任意のレベルで継承を中断できます。
-
CRXDE Lite で、
/appsの下にノードを作成します。- 名前:
acs - 型:
nt:folder
- 名前:
-
/apps/acsの下にノードを作成します。- 名前:
analytics - 型:
sling:Folder
- 名前:
-
/apps/acs/analyticsの下にノードを 2 つ作成します。- 名前:components
- 型:
sling:Folder
および
- 名前:templates
- タイプ:
sling:Folder
-
「
/apps/acs/analytics/components」を右クリックします。「作成…」を選択し、「コンポーネントを作成…」をクリックします。表示されるダイアログで、以下の項目を指定します。- ラベル:
googleanalyticspage - タイトル:
Google Analytics Page - スーパータイプ:
cq/cloudserviceconfigs/components/configpage - グループ:
.hidden
- ラベル:
-
「次へ」を 2 回クリックして、次の項目を指定します。
- 許可された親:
acs/analytics/templates/googleanalytics
「次へ」を 2 回クリックして、「OK」をクリックします。
- 許可された親:
-
プロパティを「
googleanalyticspage」に追加します。- 名前:
cq:defaultView - 値:
html
- 名前:
-
以下の内容で、
content.jspというファイルを/apps/acs/analytics/components/googleanalyticspageの下に作成します。code language-xml <%@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の下にノードを作成します。-
名前:items
-
型:
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/の下にテンプレートを作成します。- Resource Type =
acs/analytics/components/googleanalyticspage - Label =
googleanalytics - Title=
Google Analytics Configuration - allowedPath =
/etc/cloudservices/googleanalytics(/.*)? - allowedChildren =
/apps/acs/analytics/templates/googleanalytics - sling:resourceSuperType =
cq/cloudserviceconfigs/templates/configpage(jcr:content ノードではなくテンプレートノード) - cq:designPath =
/etc/designs/cloudservices/googleanalyticsを使用(jcr:content)
- Resource Type =
-
/apps/acs/analytics/components/googleanalyticsの下にコンポーネントを作成します。次の内容を
googleanalytics.jspに追加します。code language-xml <%@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)を入力します。「OK」をクリックします。 -
ページに移動し、「クラウドサービス」タブで、新たに作成された設定をページプロパティに追加します。
-
このページにカスタムマークアップが追加されます。