Giter Site home page Giter Site logo

kjcsb1 / silverstripe-googlecontentexperiments Goto Github PK

View Code? Open in Web Editor NEW

This project forked from heyday/silverstripe-googlecontentexperiments

0.0 1.0 0.0 24 KB

Integration with Google Content Experiments for SilverStripe.

PHP 98.61% Scheme 1.39%

silverstripe-googlecontentexperiments's Introduction

SilverStripe Google Content Experiments

Integration with Google Content Experiments for SilverStripe. Allows for Experiment Data to be integrated with your application so that experiments can be served without the usage of the GCE javascript implementation. For more information please see running a server-side experiment.

License

SilverStripe Google Content Experiments is released under the MIT license

Installation

If using SilverStripe version 2.4, please use the 1.0.0 tag

$ composer require silverstripe-googlecontentexperiments

How to use

Setup

First you must set up the correct authentication details so that Google Content Experiments can retrieve data from your Google Analytics account.

To do this:

  • Visit http://code.google.com/apis/console
  • Sign in with the account you use to manage your analytics
  • Create a new project for the website you wish to target
  • Browse to 'Services' and enable the Analytics API
  • Browse to 'API access' and click 'Create another client ID'
  • Choose 'Service account'
  • Download the private key and store it somewhere securely within your application directory

Once this is done, you will see the details for the service account. From here, you will need to copy the email address which was generated and add it to the analytics account you are targeting with 'Read & Analyze' access only. This will allow the server connection to read your experiment data.

Configuration

Once you have set up the account and downloaded the private key for your application we have to configure the service. This can be completed in your _config.php with the following. This code sets up the dependancy injection container which is used.

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
    );

};

Generally the following applies:

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

You also need to include the GoogleContentExperimentScripts somewhere on the page(s) you want to test.

<% include GoogleContentExperimentsScript %>

Its also a wise move to set up a cron job which will run the processor periodically for you and pick up the new experiments so that you can then run them. This also will update variations status as the experiments progress.

0 */12 * * * /path/to/webroot/sapphire/sake GoogleContentExperimentsProcessor

Usage in templates

Templates are set up so that you can push the code without having to worry about the experiment being set up or running. This means that there is a necessary <% else %> to make sure that when experiments finish the page goes back to 'normal' until it can be updated to the winning solution.

<% 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() takes two parameters, which are separated by an underscore. The first is the VariationID we are checking for, the second is the ExperimentID (internal ID). The above example shows three variation possibilities for ExperimentID 1.

If you are only running one experiment per page, the ExperimentID is not needed, e.g. <% if GoogleContentExperimentVariation(0) %>

Contributing

Unit Testing

$ composer install --prefer-dist --dev
$ phpunit

Code guidelines

This project follows the standards defined in:

silverstripe-googlecontentexperiments's People

Contributors

camspiers avatar stevie-mayhew avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.