heyday / silverstripe-googlecontentexperiments
Requires
- php: >=5.3.2
- composer/installers: ~1.0
- nyholm/google-api-php-client: ~0.1
- pimple/pimple: ~1.0
Requires (Dev)
- pdepend/pdepend: ~1.1
- phploc/phploc: dev-master
- phpmd/phpmd: ~1.4
- phpunit/phpunit: ~3.7
- sami/sami: dev-master
- symfony/class-loader: ~2.2
This package is auto-updated.
Last update: 2024-08-29 04:14:23 UTC
README
集成 Google Content Experiments for SilverStripe。允许将实验数据集成到您的应用程序中,以便在无需使用 GCE JavaScript 实现的情况下提供实验。有关更多信息,请参阅运行服务器端实验。
许可
SilverStripe Google Content Experiments 在MIT 许可下发布
安装
If using SilverStripe version 2.4, please use the 1.0.0 tag
$ composer require silverstripe-googlecontentexperiments
如何使用
设置
首先,您必须设置正确的认证信息,以便 Google Content Experiments 可以从您的 Google Analytics 账户中检索数据。
为此
- 访问 http://code.google.com/apis/console
- 使用您用于管理分析账户的账户登录
- 为要针对的网站创建一个新项目
- 浏览到 '服务' 并启用 Analytics API
- 浏览到 'API访问' 并点击 '创建另一个客户端ID'
- 选择 '服务帐户'
- 下载私钥并将其安全地存储在您的应用程序目录中
完成此操作后,您将看到服务帐户的详细信息。从这里,您需要复制生成的电子邮件地址,并将其添加到具有仅 '读取与分析' 访问权限的目标分析账户中。这将允许服务器连接读取您的实验数据。
配置
一旦您已设置帐户并下载了应用程序的私钥,我们必须配置服务。这可以通过以下方式在您的 _config.php
中完成。此代码设置依赖注入容器,它被使用。
use Heyday\GoogleContentExperiments; $container = GoogleContentExperiments\Container::getInstance(); $container['google_content_experiments.class'] = 'Heyday\GoogleContentExperiments\GoogleContentExperiments'; $container['google_content_experiments.config.project_id'] = 'PROJECT_ID'; $container['google_content_experiments.config.web_property_id'] = 'WEB_PROPERTY_ID'; $container['google_content_experiments.config.profile_id'] = 'PROFILE_ID'; $container['google_analytics_service'] = function ($c) { $client = new \GoogleApi\Client( array( 'application_name' => 'APPLICATION_NAME', 'oauth2_client_id' => 'CLIENT_ID', 'use_objects' => true ) ); $client->setAssertionCredentials( new \GoogleApi\Auth\AssertionCredentials( 'EMAIL_ADDRESS', array('https://www.googleapis.com/auth/analytics.readonly'), // SCOPE file_get_contents( BASE_PATH . PATH_TO_SERVER_KEY ) ) ); return new \GoogleApi\Contrib\AnalyticsService( $client ); };
通常以下适用
PROJECT_ID: available from your Analytics account
WEB_PROPERTY_ID: available from your Analytics account - usually takes the form 'UA-11111111-11'
PROFILE_ID: available from your Analytics account
APPLICATION_NAME: the name of your Application
CLIENT_ID: available from the Google APIs console
SCOPE: Scope needed for the application - usually only 'https://www.googleapis.com/auth/analytics.readonly'
EMAIL_ADDRESS: the email address which we previously added to our Analytics account
您还需要在要测试的页面(s)的某个位置包含 GoogleContentExperimentScripts。
<% include GoogleContentExperimentsScript %>
设置定时任务也是一个明智的选择,该任务将定期运行处理器并获取新实验,以便您可以运行它们。这还将根据实验进度更新变体状态。
0 */12 * * * /path/to/webroot/sapphire/sake GoogleContentExperimentsProcessor
在模板中使用
模板已经设置,您可以推送代码,无需担心实验的设置或运行。这意味着有必要在 <% else %>
中添加必要的代码,以确保实验完成后,页面返回到 '正常' 状态,直到可以更新为获胜方案。
<% if GoogleContentExperiment %>
<% if GoogleContentExperimentVariation(0_1) %>
THIS IS THE ORIGINAL VARIATION
<% end_if %>
<% if GoogleContentExperimentVariation(1_1) %>
THIS IS ALTERNATIVE VARIATION
<% end_if %>
<% if GoogleContentExperimentVariation(2_1) %>
THIS IS ALTERNATIVE VARIATION
<% end_if %>
<% else %>
NO EXPERIMENT IS RUNNING RETURN TO REGULAR SERVICES
<% end_if %>
GoogleContentExperimentVariation()
接受两个参数,由下划线分隔。第一个是我们检查的变体ID,第二个是实验ID(内部ID)。上面的例子显示了实验ID 1 的三个变体可能性。
如果您在页面上仅运行一个实验,则不需要实验ID,例如 <% if GoogleContentExperimentVariation(0) %>
贡献
单元测试
$ composer install --prefer-dist --dev
$ phpunit
代码指南
此项目遵循以下定义的标准