ホームページ >バックエンド開発 >PHPチュートリアル >Building OctuneCMSプラグイン:Google Analytics
OctureCMSプラグインの開発:Google Analyticsの例
OctureCMSは急速に成長しているコンテンツ管理システム(CMS)であり、すべてのCMSプラットフォームと同様に、その機能はプラグインによって強化されています。この記事では、Googleアナリティクスの統合を実用的な例として使用して、TurteCMSプラグインを作成するための基礎ガイドを提供します。
重要な概念:
)をページまたはパルティアルに挿入します。 完全なコードはgithubで利用できます。
プラグインマネジメント:{% component 'gaCode' %}
フォルダーにあります。 管理は介して処理されます:
system>更新:plugins
このページでは、プロジェクトを添付し(10月のアカウントが必要)、マーケットプラグインの追加、ウェブサイトの更新が可能です。
Author.PluginName
プラグインの作成が合理化されています:plugins
を使用します。 内部では、RAFIE.GoogleAnalyticsCode
ファイルを作成します。Plugin.php
php artisan create:plugin RAFIE.GoogleAnalyticsCode
ファイルを含むuploads
フォルダーを含む、必要なファイルとフォルダーが自動的に生成されます。
version.yaml
メソッド(オーバーライドPlugin.php
)はプラグインを定義します:pluginDetails()
SystemClassesPluginBase
<code class="language-php">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' ]; }</code>
>
コンポーネントでページとのやり取りを有効にします。 手動で(フォルダー内)またはコマンドラインを介して作成します:
components
<code class="language-bash">php artisan create:component RAFIE.GoogleAnalyticsCode GoogleAnalytics</code>
メソッド(オーバーライド
'scomponentDetails()
メソッドでコンポーネントを登録します:CmsClassesComponentBase
Plugin.php
registerComponents()
コンポーネントは最初に
<code class="language-php">public function registerComponents() { return [ 'RAFIE\GoogleAnalyticsCode\Components\GoogleAnalytics' => 'gaCode' ]; }</code>をレンダリングします
これをGoogle Analyticsトラッキングコードに置き換えます:default.htm
<code class="language-html"><p>></p>This is the default markup for component GoogleAnalytics> <small>></small>You can delete this file if you want></code>テーマに応じて、スクリプトをページの最後に理想的に配置する必要があることを忘れないでください。 コンポーネントのプロパティと設定:
<code class="language-html">(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');</code>
トラッキングIDをハードコードする代わりに、コンポーネントプロパティを使用します:
:
を使用してプロパティを定義します
<code class="language-twig">{% component 'gaCode' code='UA-12345678-1' %}</code>
を使用して、値をページに渡す:defineProperties()
<code class="language-php">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' ] ]; }</code>よりユーザーフレンドリーなアプローチについては、プラグインの設定を使用してください:
onRender()
<code class="language-php">public function onRender() { $this->page['code'] = $this->property('code'); }</code>
create
:
:models/GoogleAnalyticsSettings.php
<code class="language-php">class GoogleAnalyticsSettings extends Model { public $implement = ['System.Behaviors.SettingsModel']; public $settingsCode = 'rafie_google_analytics_code'; public $settingsFields = 'fields.yaml'; }</code>
:models/fields.yaml
の登録設定
<code class="language-yaml">fields: code: label: Your Google Analytics ID placeholder: UA-XXXXXXXX-X</code>
Plugin.php
<code class="language-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 ] ]; }</code>:
で設定を取得します
結論:onRender()
<code class="language-php">public function onRender() { $settings = GoogleAnalyticsSettings::instance(); $this->page['code'] = $settings->code; }</code>OctureCMSは、堅牢で柔軟なプラグイン開発システムを提供します。 この例は、核となる原則を示しています。ソースコードとコミュニティリソースのさらなる調査により、理解が向上します。
よくある質問(FAQS):(提供されたFAQはすでに十分に構成されており、包括的です。変更は必要ありません。
以上がBuilding OctuneCMSプラグイン:Google Analyticsの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。