Skip to content

Latest commit

 

History

History
607 lines (459 loc) · 21.6 KB

README.md

File metadata and controls

607 lines (459 loc) · 21.6 KB

Campaign

Campaign is a simple tool for creation and management of banner campaigns on your web. It allows you to create banner campaigns without knowledge of HTML/CSS/JS, A/B test the banners and in combination with REMP Beam (for providing statistics) display and evaluate performance of your campaigns.

Admin (Laravel)

Campaign Admin serves as a tool for configuration of banners and campaigns. It's the place for UI generation of banners and definition of how and to whom display Campaigns.

When the backend is ready, don't forget to create .env file (use .env.example as boilerplate), install dependencies and run DB migrations:

# 1. Download PHP dependencies
composer install

# 2. Download JS/HTML dependencies
yarn install

# !. use extra switch if your system doesn't support symlinks (Windows; can be enabled)
yarn install --no-bin-links

# 3. Generate assets
yarn run all-dev // or any other alternative defined within package.json

# 4. Run migrations
php artisan migrate

# 5. Generate app key
php artisan key:generate

# 6. Run seeders (optional)
php artisan db:seed

Dependencies

  • PHP ^7.1.3
  • MySQL ^5.7.8
  • Redis ^3.2

Deployment

Commands

For application to function properly you need to run php artisan campaigns:refresh-cache command every time the application change is deployed or application configuration (such as .env) is changed.

Schedule

For application to function properly you need to add Laravel's schedule running into your crontab:

* * * * * php artisan schedule:run >> storage/logs/schedule.log 2>&1

Laravel's scheduler currently includes:

CacheSegmentJob:

  • Triggered hourly and forced to refresh cache segments.

AggregateCampaignStats:

  • Triggered every minute, saves statistics about ongoing campaings from Beam Journal (if configured).

Queue

For application to function properly, you also need to have Laravel's queue worker running as a daemon. Please follow the official documentation's guidelines.

php artisan queue:work

Admin integration with CMS/CRM

Javascript snippet

Include following snippet into the page to process campaigns and display banners. Update rempConfig object as needed.

Note: To automatically track banner events to BEAM Tracker, add also tracker property to rempConfig object. See BEAM README for details. The two snippets complement each other and can be combined into one big JS snippet including Campaign and Beam functionality.

(function(win, doc) {
    function mock(fn) {
        return function() {
            this._.push([fn, arguments])
        }
    }
    function load(url) {
        var script = doc.createElement("script");
        script.type = "text/javascript";
        script.async = true;
        script.src = url;
        doc.getElementsByTagName("head")[0].appendChild(script);
    }
    win.remplib = win.remplib || {};
    var mockFuncs = {
        "campaign": "init",
        "tracker": "init trackEvent trackPageview trackCommerce",
        "iota": "init"
    };

    Object.keys(mockFuncs).forEach(function (key) {
        if (!win.remplib[key]) {
            var fn, i, funcs = mockFuncs[key].split(" ");
            win.remplib[key] = {_: []};

            for (i = 0; i < funcs.length; i++) {
                fn = funcs[i];
                win.remplib[key][fn] = mock(fn);
            }
        }
    });
    // change URL to location of CAMPAIGN remplib.js
    load("http://campaign.remp.press/assets/lib/js/remplib.js");
})(window, document);

var rempConfig = {
    // UUIDv4 based REMP BEAM token of appropriate property
    // (see BEAM Admin -> Properties)
    // required if you're using REMP segments
    token: String,
    
    // optional, identification of logged user
    userId: String,
    
    // optional, flag whether user is currently subscribed to the displayed content 
    userSubscribed: Boolean,
    
    // optional, this is by default generated by remplib.js library and you don't need to override it
    browserId: String,
    
    // optional, controls where cookies (RTM - REMP's UTM parameters of visit) are stored
    cookieDomain: ".remp.press",

    // optional, controls which type of storage should be used by default (`local_storage` or `cookie`)
    // default is `local_storage`
    storage: "local_storage", 

    // optional, specifies how long to store specific keys in storage (in minutes)
    storageExpiration: {
        // default value (in minutes) for all storage keys if not overriden in `keys`
        "default": 15,
        // specific pairs of key name and life length in minutes
        "keys": {
            "browser_id": 1051200, // 2 years in minutes
            "campaigns": 1051200
        }
    },

    // required, Campaign specific options
    campaign: {
        // required, URL host of REMP Campaign
        url: "http://campaign.remp.press",
        
        // Additional params that will be appended links within displayed banner
        //
        // Key represents variable name, value should be defined as callback returning string response.
        // Following example will be appended as "&foo=bar&baz=XXX".
        // If the value is not function, remplib validation will throw an error and won't proceed further.
        bannerUrlParams:  {
            "foo": function() { return "bar" },
            "baz": function() { return "XXX" }
        },
        
        variables: {
            // variables replace template placeholders in banners,
            // e.g. {{ email }} -> [email protected]
            //
            // the callback doesn't pass any parameters, it's required for convenience and just-in-time evaluation
            //
            // missing variable is translated to empty string
            email: {
                value: function() {
                    return "[email protected]"
                }
            },
        },
        
        // Optional. Pageview attributes are used to provide to Campaign additional information about the pageview.
        // You can configure your campaigns to be displayed based on these attributes - see "Pageview attributes"
        // section when editing the campaign.
        //
        // The value can be:
        //   - string: Campaign is displayed when the string matches configured value for given attribute name.
        //   - array of strings: Campaign is displayed, when one of the provided strings matches the configured value.
        //
        // Any other kind of value is ignored and triggers JS console warning.
        //
        // All of provided attributes which are not configured in the campaign are ignored during processing.
        pageviewAttributes: {
            "attribute_1": "value_1",
            "attribute_2": ["value_1", "value_2"]
        }
    }
    
    // if you want to automatically track banner events to BEAM Tracker,
    // add also rempConfig.tracker property
    //
    // see REMP BEAM README.md
    
};
remplib.campaign.init(rempConfig);
Single-page applications

If you use single-page application and need to reinitialize JS library after it's been loaded:

  1. Update the rempConfig variable to reflect the navigation changes.
  2. Call remplib.campaign.init(rempConfig) again to reinitialize the JS tracking state. All existing banners will hide and campaigns will be evaluated again.
Segment integration

To determine who to display a campaign to, Campaign is dependent on user segments - effectively lists of user/browser IDs which should see a banner. You can register as many segment providers as you want, the only condition is that the providers should work with the same user-base (one user ID has to always point to the) same user.

The implementation is required to implement App\Contracts\SegmentContract interface.

All registered implementations are hidden behind facade of SegmentAggregator. This facade is then used to display available segments in configuration listings and to evaluate actual members of segments during campaign runtime.

If you want to link the Campaign to your own system, these are the methods to implement:

  • provider(): string: Uniquely identifies segment provider among other segment providers. This is internally required to namespace segment names in case of same segment name being used in multiple segment sources.

    return "my-provider"; 
  • list(): Collection: Returns collection of all segments available for this provider. The structure of response is:

    return [
        [
            'name' => String, // user friendly label
            'provider' => String, // should be same as result of provider()
            'code' => String, // machine friendly name, slug
            'group' => [
                'id' => Integer, // ID of segment group
                'name' => String, // user friendly label of group
                'sorting' => Integer // sorting index; lower the number, sooner the group appears in the list 
            ]
        ],
    ];
  • users(CampaignSegment $segment): Collection: Returns list of user IDs belonging to the segment.

    • $segment: Instance of CampaignSegment holding the reference to segment used in a campaign.

    The response is than expected to be collection of integers/strings representing user IDs:

    return collect([
        String,
        String,
        // ...
    ])
  • checkUser(CampaignSegment $campaignSegment, string $userId): bool: Checks whether given userId belongs to the provided campaignSegment. This can either be done against external API (if it's prepared for realtime usage) or against cached list of user IDs - it's internal to the implementation. If the implementation doesn't support user-based segments, return false.

  • checkBrowser(CampaignSegment $campaignSegment, string $browserId): bool: Checks whether given browserId belongs to the provided campaignSegment. This can either be done against external API (if it's prepared for realtime usage) or against cached list of browser IDs - it's internal to the implementation. If the implementation doesn't support browser-based segments, return false.

  • cacheEnabled(): bool: Flag whether the segments of this provider used in active campaigns should be cached by the application or not. If true, Campaign will every hour request users() for each active Campaign and stores the collection of user IDs to the application cache.

  • getProviderData(): Provider can pass arbitrary data back to the user's browser. This can be leveraged to cache browser/user related information without affecting the backend system. This data can be then altered by JS in the frontend application (if needed). All provided data is passed back to the server on each campaign/showtime request - on each banner display attempt.

    Note: We use this kind of caching to count number of trackable events directly in browser. Segment rules based on these events are then evaluated based on the data provided in cache instead of hitting database. This method dramatically improved performance of REMP Beam event database (Elasticsearch).

  • setProviderData($providerData): void: Complementary providerData method to store back the data provided by frontend application in incoming request.

When implemented, create a segment provider providing the instance of your implementation. The provider should set \App\Contracts\SegmentAggregator::TAG to the class so your implementation would get also registered to the SegmentAggregator.

Admin integration with Beam Journal

Beam Journal API (also known as Segments API) provides API for retrieving information about ongoing campaigns. Its integration with Campaign tool is optional, but provides ability to see campaign statistics directly in the Campaign admin interface.

Information on how to set up Journal API can be found in the documentation or in the REMP installation guidelines.

Once Journal API is running, you can enable its integration by pointing REMP_SEGMENTS_ADDR to Journal API URL in .env configuration file.

Laravel's queue currently includes

CacheSegmentJob:

  • Triggered when campaign is activated.
  • Trigerred when cached data got invalidated and need to be fetched again.

If the data are still valid, job doesn't refresh them.

Variables

Campaign supports use of variables in your banner template contents, custom javascript and custom css. You can create variables using Add new variable in Variables main menu section.

You can use created variables by adding {{ variable_name }} to one of the fields used in banner content or in custom javascript & css.

API Documentation

Campaign provides a simple API for several tasks described bellow.

All examples use http://campaign.remp.press as a base domain. Please change the host to the one you use before executing the examples.

All examples use XXX as a default value for authorization token, please replace it with the real token API token that can be acquired in the REMP SSO.

All requests should contain (and be compliant) with the follow HTTP headers.

Content-Type: application/json
Accept: application/json
Authorization: Bearer REMP_SSO_API_TOKEN

API responses can contain following HTTP codes:

Value Description
200 OK Successful response, default value
202 Accepted Successful response, accepted for processing
400 Bad Request Invalid request (missing required parameters)
403 Forbidden The authorization failed (provided token was not valid)
404 Not found Referenced resource wasn't found

If possible, the response includes application/json encoded payload with message explaining the error further.


POST /api/banners/BANNER_ID/one-time-display

Campaign supports one-time display of banners to specific users. Such banner does not need associated campaign to be displayed. This can be used e.g. as a way of reminding users about required actions (instead of notifying them via email).

When calling this endpoint, please replace BANNER_ID in URL with an actual ID of the banner. Each one-time banner has to specify its expiration date and a user it shows to (targeted either via user ID or via browser ID).

Body:
{
  "user_id": "29953", // String; Required (if browser_id is not present); ID of targeted user
  "browser_id": "aaaaaa-bbbb-cccc-dddd-1111111", // String; Required (if user_id is not present); ID of targeted browser
  "expires_at": "2019-12-29T10:05:00" // Date; Required; RFC3339 formatted datetime, specifying when the one-time banner expires (it won't show after the date)
}
Examples:
curl
curl -X POST \
  http://campaign.remp.press/api/banners/[BANNER_ID]/one-time-display \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer XXX' \
  -H 'Content-Type: application/json' \
  -d '{
        "user_id": "29953",
        "expires_at": "2019-12-29T10:05:00"
}'
raw PHP
$payload = [
    "user_id" => "29953",
    "expires_at" => "2019-12-29T10:05:00"
];
$jsonPayload = json_encode($payload);
$context = stream_context_create([
        'http' => [
            'method' => 'POST',
            'header' => "Content-Type: type=application/json\r\n"
                . "Accept: application/json\r\n"
                . "Content-Length: " . strlen($jsonPayload) . "\r\n"
                . "Authorization: Bearer XXX",
            'content' => $jsonPayload,
        ]
    ]
);
$bannerId = 1;
$response = file_get_contents("http://campaign.remp.press/api/banners/{$bannerId}/one-time-display", false, $context);
// process response (raw JSON string)
Response:

Valid response with 202 HTTP code:

{
  "status": "ok"
}
Override user's presence in segment's cache
  • Add user: POST /api/segment-cache/provider/{segment_provider}/code/{segment_code}/add-user
  • Remove user: POST /api/segment-cache/provider/{segment_provider}/code/{segment_code}/remove-user

If segment provider supports it, Campaign will cache members of segment for faster access. If segment is dynamic (changed often), it can lead to incorrect campaign targeting (old data).

Note: Interval for cache invalidation is set to one hour (check App\Console\Kernel->schedule()).

These two API endpoints allow overriding membership of one user in cached segment from outside of Campaign (eg. from CRM).

Both endpoints return response 404 Not found if segment is not actively used by active or scheduled campaign.

API path parameters:
Parameter Type Required Description
segment_provider string yes Segment's provider returned by SegmentContract->provider().
Eg. out of box contracts: crm_segment/pythia_segment/remp_segment.
segment_code string yes Code which identifies segment (stored in campaign_segments.code).
Body:
{
  "user_id": "29953", // String; Required; ID of targeted user
}
Examples:
curl

Path parameters:

  • segment_provider - crm_segment
  • segment_code - example_testing_segment
curl -X POST \
  http://campaign.remp.press/api/segment-cache/provider/crm_segment/code/example_testing_segment/add-user \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer XXX' \
  -H 'Content-Type: application/json' \
  -d '{
        "user_id": "29953"
}'
raw PHP
$payload = [
    "user_id" => "29953",
];
$jsonPayload = json_encode($payload);
$context = stream_context_create([
        'http' => [
            'method' => 'POST',
            'header' => "Content-Type: type=application/json\r\n"
                . "Accept: application/json\r\n"
                . "Content-Length: " . strlen($jsonPayload) . "\r\n"
                . "Authorization: Bearer XXX",
            'content' => $jsonPayload,
        ]
    ]
);
$segmentProvider = "crm_segment";
$segmentCode = "example_testing_segment";
$response = file_get_contents("http://campaign.remp.press/api/segment-cache/provider/{$segmentProvider}/code/{$segmentCode}/add-user", false, $context);
// process response (raw JSON string)
Response:

Valid response with 202 HTTP code:

{
  "status": "ok"
}

Response with 404 HTTP code:

{
  "status": "error",
  "message": "Segment with code [example_testing_segment] from provider [crm_segment] is not actively used in the campaign."
}

Healthcheck

Route http://campaign.remp.press/health provides health check for database, Redis, storage and logging.

Returns:

  • 200 OK and JSON with list of services (with status "OK").

  • 500 Internal Server Error and JSON with description of problem. E.g.:

    {
      "status":"PROBLEM",
      "log":{
        "status":"PROBLEM",
        "message":"Could not write to log file",
        "context": // error or thrown exception...
      //...
    }
    

MaxMind - GeoIP2 Lite

This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com.

Newsletter Banner

This banner type allows you to directly collect newsletter subscribers.

In most cases you are going to need your own API proxy, since most newsletter APIs (including REMP CRM) make use of private tokens that should not be exposed to client. In case your newsletter API does not require such, you might not need a proxy.

Setup

All configuration options are set via .env, refer to .env.example for full list of available options.

API requirements

Newsletter API is required to respond with proper HTTP response code. On both success and failure, there is and custom event emitted. See next section for details.

Custom Events

Custom events rempNewsletterSubscribeError or rempNewsletterSubscribeSuccess are available if requests are configured with XHR. Events are fired on form element. Event contains following items in detail prop:

field type description
type String response - if there is a valid XHR response
exception - for general error, e.g. connection error
response Object fetch response object, applicable for response type
message String error message, applicable for exception type

Error handling example

window.addEventListener("rempNewsletterSubscribeFailure", function(event){
    if (event.detail && event.detail.type === 'response'){
        const response = event.detail.response;
        console.warn('HTTP Status Code:', response.status);
        
        // to retrieve body use `json()` or `text()` depending on your API implemetation
        response.json().then(function(data) {
            console.warn(data);
        });
    }

    if (event.detail && event.detail.type === 'exception') {
        console.warn(event.detail.message);
    }
});

RTM tracking

Following params are added to every request or links (e.g. T&C):

  • rtm_source: "remp_campaign" => always same string
  • rtm_medium: displayType => overlay or inline
  • rtm_campaign: campaignUuid => campaign id
  • rtm_content: uuid => banner id
  • banner_variant: variantUuid => banner variant id (in campaign)