> coctcms插件開發:Google Analytics(分析)示例
密鑰概念:
通過插件的插件管理:
{% component 'gaCode' %}
此頁面允許附加項目(需要十月cms帳戶),添加市場插件並更新您的網站。
命令行:。
plugins
plugins
>目錄中創建一個新文件夾。該文件夾名稱充當您的唯一名稱空間(在OctiotCMS網站上註冊您的名稱空間以避免衝突)。 在此示例中,我們將使用RAFIE.GoogleAnalyticsCode
。 在內部,創建一個Plugin.php
文件。 php artisan create:plugin RAFIE.GoogleAnalyticsCode
uploads
version.yaml
文件的
Plugin.php
如果未出現插件,請單擊“檢查更新”以刷新列表。 pluginDetails()
>
SystemClassesPluginBase
public function pluginDetails() { return [ 'name' => 'Google Analytics Code', 'description' => 'Insert Google Analytics tracking code into your pages', 'author' => 'RAFIE Younes', 'icon' => 'icon-bar-chart-o' ]; }
>組件啟用與頁面的交互。 手動創建它們(在
文件夾中)或通過命令行:>
components
php artisan create:component RAFIE.GoogleAnalyticsCode GoogleAnalytics
>'s 方法中:
componentDetails()
組件最初渲染CmsClassesComponentBase
:Plugin.php
registerComponents()
public function registerComponents() { return [ 'RAFIE\GoogleAnalyticsCode\Components\GoogleAnalytics' => 'gaCode' ]; }
default.htm
請記住,理想情況下應將腳本放在頁面末尾,具體取決於您的主題。
<p>></p>This is the default markup for component GoogleAnalytics> <small>></small>You can delete this file if you want>
組件屬性和設置:
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', '{{ code }}', 'auto'); ga('send', 'pageview');
而不是硬編碼跟踪ID,而是使用組件屬性:
使用來定義屬性:
>使用
將值傳遞到頁面:{% component 'gaCode' code='UA-12345678-1' %}
defineProperties()
對於更易於用戶友好的方法,請使用插件設置:
public function defineProperties() { return [ 'code' => [ 'title' => 'Google Analytics tracking code', 'description' => 'Your Google Analytics tracking code', 'default' => '', 'type' => 'string', 'validationPattern' => '^UA-\d{4,9}-\d{1,4}$', 'validationMessage' => 'Invalid tracking code', 'placeholder' => 'UA-XXXXXXX' ] ]; }
onRender()
創建
public function onRender() { $this->page['code'] = $this->property('code'); }
創建
:
models/GoogleAnalyticsSettings.php
中的註冊設置:
class GoogleAnalyticsSettings extends Model { public $implement = ['System.Behaviors.SettingsModel']; public $settingsCode = 'rafie_google_analytics_code'; public $settingsFields = 'fields.yaml'; }
models/fields.yaml
fields: code: label: Your Google Analytics ID placeholder: UA-XXXXXXXX-X
中檢索設置:Plugin.php
public function registerSettings() { return [ 'settings' => [ 'label' => 'Google Analytics Code', 'description' => 'Manage Google Analytics Settings', 'icon' => 'icon-bar-chart-o', 'class' => 'RAFIE\GoogleAnalyticsCode\Models\GoogleAnalyticsSettings', 'order' => 1 ] ]; }
結論: >經常詢問問題(常見問題解答):(所提供的常見問題解答已經結構良好。
以上是構建OctectCMS插件:Google Analytics(分析)的詳細內容。更多資訊請關注PHP中文網其他相關文章!