重要事項:本文件不適用於您目前選取的格式 email!
amp-link-rewriter
說明
允許發布商根據可設定的模式改寫網址
必要腳本
<script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js"></script>
支援版面配置
用法
amp-link-rewriter
允許發布商根據可設定的模式改寫網址。
必要元素結構
新增必要的 script
在 AMP 頁面的 <head>...</head>
區段中,在 <script async src="https://cdn.ampproject.org/v0.js"></script>
行之前插入下列程式碼。
<script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js" ></script>
新增 amp-link-rewriter
元件
在 AMP 頁面的 <body>...</body>
區段中,插入如下範例所示的程式碼 (必須針對每個供應商的使用案例進行客製化)
<amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"], "attribute": { "href": "((?!(https:\/\/skip\.com)).)*", "id": "comments", "class": "sidebar", "rel": "(?!(skip))*" }, "vars": { "customerId": "12345" } } </script> </amp-link-rewriter>
完整範例
最終程式碼應如下所示
<!DOCTYPE html> <html ⚡ lang="en"> <head> ... <script async custom-element="amp-link-rewriter" src="https://cdn.ampproject.org/v0/amp-link-rewriter-0.1.js" ></script> ... <script async src="https://cdn.ampproject.org/v0.js"></script> </head> <body> ... <amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"], "attribute": { "href": "`((?!(https:\/\/skip\.com)).)*`", "id": "comments", "class": "sidebar", "rel": "(?!(skip))*" }, "vars": { "customerId": "12345" } } </script> </amp-link-rewriter> .... </body> </html>
JSON 設定
output
(必要)
output
屬性必須是字串值,其中包含重新導向網址,以及將以設定 JSON vars
屬性或錨點本身中定義的值取代的預留位置查詢字串。
<amp-link-rewriter layout="nodisplay"> <script type="application/json"> { "output": "https://visit.foo.net?pid=110&cid=${customerId}`", "vars": { "customerId": "12345" } } </script> </amp-link-rewriter>
我們也可以在錨點資料屬性中定義資料值,如下列範例所示
<a href="https://www.amazon.de/s?i=computers&rh=n%3A340843031&ref=sn_gfs_co_computervs_AM_5" data-vars-event-id="231" ></a>
設定可能如下所示
{ "output": "https://visit.foo.net?eid=${eventId}&cid=${customerId}" }
重新改寫後的網址結果將會是
`https://visit.foo.net?eid=231&cid=12345`
除了在 JSON 設定的 vars
屬性中定義的變數,或作為資料屬性之外,還有其他預先定義的 vars
,即 AMP URL MACRO 以及可用於取代輸出模式中值的錨點屬性 id
和 href
。可用的 AMP URL MACRO 為 AMP_GEO
、DOCUMENT_REFERRER
和 SOURCE_URL
。下表顯示已定義資料與預留位置之間的關係。
值 | 來源 | 範例 | 預留位置 |
---|---|---|---|
國家/地區 | geo | us |
AMP_GEO(ISOCountry) |
data-vars-* | 錨點 | <a href="..." data-vars-merchant-id="123" /> |
${merchantId} |
href | 錨點 | <a href="https://retailer.com" /> |
${href} |
id | 錨點 | <a href="..." id="link" /> |
${id} |
location | 頁面 | https://www.partnerweb.com/ |
SOURCE_URL |
random | 頁面 | Math.random().toString(32).substr(2) |
${random} |
referrer | 頁面 | https://google.de/ |
DOCUMENT_REFERRER |
rel | 錨點 | <a href="..." rel="pass" /> |
${rel} |
rev | 錨點 | <a href="..." rev="author" /> |
${rev} |
vars.* | 設定 | { "vars": { "publisherId": "123" } } |
${publisherId} |
section (選填)
section
屬性定義 CSS 選取器運算式陣列,用於篩選應執行網址改寫的區域。
{ "output": "https://visit.foo.net?pid=110&url=${href}&cid=${customerId}", "section": ["#product-listing-1", "#product-listing-2"] }
在先前的範例中,以屬性 ID 定義為 product-listing-1
和 product-listing-2
的 HTML 區段將會被納入網址改寫的考量。
attribute (選填)
attribute
屬性定義規則清單,以比對從篩選區段擷取的錨點元素。這些規則是從與 HTML 元素屬性 (例如 id
、href
、class
或 rel
) 相關聯的規則運算式建構而來。
{ "section": ["#product-listing-1"], "attribute": { "href": "((?!(https://skip.com)).)*", "class": "comments" } }
ID 為 product-listing-1
的 HTML 區域內的錨點必須符合針對 href 和 class 屬性定義的規則運算式。規則運算式將以 ^
和 \$
包裝以進行完整比對。
在此範例中,這表示所有包含 youtube.com
和 mobile.vodafone.de
的錨點都會被排除。此外,包含的錨點需要具有值為 comments
的 class 屬性。
scopeDocument (選填)
依預設,如果頁面沒有任何連結符合設定中指定的 attribute
和 section
範圍,則所有錨點都會被改寫。
若要變更此行為,您需要將 scopeDocument
設定為 false
。
驗證
請參閱 AMP 驗證器規格中的 amp-link-rewriter
規則。
您已閱讀本文件十幾次,但它似乎沒有真正涵蓋您的所有問題嗎?或許其他人也有相同的感受:在 Stack Overflow 上與他們交流。
前往 Stack Overflow 發現錯誤或缺少功能?AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的持續參與者,但我們也歡迎針對您特別熱衷的問題提供一次性的貢獻。
前往 GitHub