Search code examples
phpamazon-web-servicesamazon-personalize

"Amazon Personalize" PutItems, PutEvents, PutUsers using aws/aws-sdk-php


I am building an integration between my Laravel application and Amazon Personalize using:

aws/aws-sdk-php 

Everything goes ok, but when I look on how to update the datasets with new Users, interactions and items, I couldn't find the right method/approach to do this, or if it is even possible.

I have created the Event Tracker but I can't find how to replicate this Python code into PHP:

    # Configure Properties:
    event = {
    "itemId": str(ITEM_ID),
    }
    event_json = json.dumps(event)
        
    # Make Call
    personalize_events.put_events(
    trackingId = TRACKING_ID,
    userId= USER_ID,
    sessionId = session_ID,
    eventList = [{
        'sentAt': int(time.time()),
        'eventType': 'EVENT_TYPE',
        'properties': event_json
        }] 

The code above is a portion extracted from here https://github.com/aws-samples/amazon-personalize-samples/blob/master/getting_started/notebooks/1.Building_Your_First_Campaign.ipynb

That would be for tracking new events: https://docs.aws.amazon.com/personalize/latest/dg/API_UBS_PutEvents.html

If there is a chance to avoid executing an extra Python script better, if not I will go for that option.

Thanks in advance!


Solution

  • I found that I need to use the PersonalizeEventsClient instead of PersonalizeClient for this purpose, as stated here:

    https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-personalize-events-2018-03-22.html#putevents

    The link is part of the AWS Personalize documentation, I missed that previously, there they explain how to PutEvents, Items and Users using the AWS SDK PHP, for example:

    $client = AWS::createClient('personalizeevents');
    $result = $client->putEvents([
        'eventList' => [ // REQUIRED
            [
                'eventId' => '<string>',
                'eventType' => '<string>', // REQUIRED
                'eventValue' => <float>,
                'impression' => ['<string>', ...],
                'itemId' => '<string>',
                'properties' => '<string>',
                'recommendationId' => '<string>',
                'sentAt' => <integer || string || DateTime>, // REQUIRED
            ],
            // ...
        ],
        'sessionId' => '<string>', // REQUIRED
        'trackingId' => '<string>', // REQUIRED
        'userId' => '<string>',
    ]);
    

    I am also using this service provider for Laravel:

    https://github.com/aws/aws-sdk-php-laravel