Follow

CSV: Creating Point Activities with CSV

Getting Ready

Follow the instructions from the first two sections of the Creating Your First User article to setup and configure the Mambo PHP SDK.

The Script

Below you will find a script that loads points based activities into Mambo based on a CSV file. The CSV file can also be found below so that you can follow the same structure for your data. Note that both files are also available for download.

In order to use the script you will need to take the following steps:

  • Configure the fields in the script that require your input (see below)
  • Add your behaviour activities to the CSV file
  • Setup the Mambo PHP SDK as explained in Creating Your First User

There are 4 fields in the script that require your input:

Once everything is configured you can run your CSV loader with the following command:

php point_loader.php sample_point_data.csv

The point_loader.php script:

<?php

// Import the library files
require_once('sdk/Mambo.php');


// --------------- CONFIGURATION START ----------------

// Configure server and security variables
$serverUrl = "INSERT_MAMBO_SERVER_URL";
$publicKey = "INSERT_MAMBO_PUBLIC_KEY";
$privateKey = "INSERT_MAMBO_PRIVATE_KEY";
$siteUrl = "INSERT_MAMBO_SITE_URL";

// --------------- CONFIGURATION END ----------------


// Initialise the clients credentials and end point URL, this only needs to be done once
MamboClient::setCredentials( $publicKey, $privateKey );
MamboClient::setEndPointBaseUrl( $serverUrl );

// Use default timezone to parse dates
date_default_timezone_set("UTC");


// Check if a CSV file was supplied
if( $argc != 2 ) {
exit("Please provide a CSV file name as an argument i.e. php point_loader my_file.csv");
}


// Open the CSV file
$csvFileName = $argv[1];
$handle = fopen( $csvFileName, "r" );

if( $handle === FALSE ) {
exit("Failed to open the CSV file: " + $csvFileName);
}


// Process the CSV file
$row = 0;

while( ( $data = fgetcsv( $handle, 1000, "," ) ) !== FALSE )
{
$row++;

debugLog( "---------------------------------" );
debugLog( "Parsing row number: " . $row );

if( $row == 1 ) {
debugLog( "Head row: skipping" );
continue;
}

createActivity( $data );

debugLog( "Completed row number: " . $row );
}

// Close the CSV file
fclose( $handle );
exit();


// --------------- Helper Functions ----------------


// Try to create the activity
function createActivity( $data )
{
global $siteUrl;

$uuid = $data[0];
debugLog( "Creating activity for user: " . $uuid );

$activityData = createActivityData( $uuid, $data );
$activity = MamboActivitiesService::createSync( $siteUrl, $activityData );
validateActivityCreation( $uuid, $activity );

return $uuid;
}


function createActivityData( $uuid, $data )
{
$pointAction = $data[1];
$pointId = $data[2];
$pointValue = $data[3];
$createdOnOverride = $data[4];
$reason = $data[5];

$points = new PointStore();
$points->setPointId( $pointId );
$points->setPoints( $pointValue );

$attrs = new ActivityPointAttrs();
$attrs->setAction( $pointAction );
$attrs->addPoint( $points );

$data = new ActivityRequestData();
$data->setUuid( $uuid );
$data->setAttrs( $attrs );

if( !empty( $createdOnOverride ) ) {
$data->setCreatedOnOverride( $createdOnOverride );
}
if( !empty( $reason ) ) {
$data->setReason( $reason );
}

return $data;
}


function validateActivityCreation( $uuid, $activity ) {
if( isset( $activity->error ) ) {
debugLog( "An unexpected error occurred! Dumping output and terminating." );
var_dump( $activity );
exit();
}
}


// Basic output to the terminal
function debugLog( $text ) {
echo $text . "\n";
}

The sample_point_data.csv:

uuid,point_action,point_id,point_value,createdOnOverride,reason
1,increment,5c9c320b2bb14c642cca010c,10,2019-01-01T13:15:00.000Z,For hitting performance metrics
2,set,5c9c320b2bb14c642cca010c,0.5,,

The fields in the CSV file represent:

  • UUID: this is the unique user ID. You can set which ever ID works best for your systems. Ideally this would be your unique corporate identity or the identity used during SSO to identify users across systems.
  • Point Action: there are 4 types of point actions that can be used in this field:
    • increment: sums the point value to the user's existing point value
    • set: overrides the user's existing point value with the one we specify
    • redeem: redeem's the user's points (only usable with redeemable points)
    • refund: refund's the user's points (only usable with redeemable points)
  • Point ID: the ID of the point we want to change. This can be found in the Mambo Administration Panel in Configuration - Points.
  • Point Value: the value / amount of points we want to action.
  • Created On Override: by default the activity will be created with today's date, but you can override and provide your own date. Please observe the pattern with which the date is used.
  • Reason: this allow you to provide a reason as to why this behaviour is being triggered. Be careful not to use commas in your text!

Download the files below:

0 Comments

Please sign in to leave a comment.