Back

Tutorials

Feb 17, 2024

How to build a Laravel Vue.js Project with Engagespot Integration?

Jobin Jose

In this tutorial, we'll guide you through setting up a Laravel Vue.js project from scratch and integrating Engagespot for personalized notifications. We'll cover installation steps for Laravel, Vue.js, and demonstrate how to create Engagespot users, send notifications, and generate user tokens.

Prerequisites Before we begin, ensure you have the following installed:

  • Composer

  • Node.js and npm

  • Laravel CLI

  • Vue CLI

  • Engagespot Account

Step 1: Setup Laravel and Vue.js

1. Install Laravel:

composer create-project --prefer-dist laravel/laravel your-project-name
cd your-project-name

2. Install Vue.js:

npm install Step

Step 2: Integrate Engagespot

Backend Integration (Laravel)

1. Create a controller to handle Engagespot operations:

php artisan make:controller EngagespotController

2. EngagespotController.php . ```php

namespace App\Http\Controllers;

use Engagespot\EngagespotClient;
use Illuminate\Http\Request;

class EngagespotController extends Controller
{
    /**
     
Create or update a user in Engagespot.*
This method will create a new user in Engagespot if the user does not exist,
or update the existing user's profile if the user already exists.*
@return array The result of the create or update operation.
*/
public function createEngagespotUser(){// Retrieve API key and secret from environment variables$apiKey = env("X_ENGAGESPOT_API_KEY");$apiSecret = env("X_ENGAGESPOT_API_SECRET");
    // Your implementation logic goes here// For example:// $engagespotClient = new EngagespotClient($apiKey, $apiSecret);// $result = $engagespotClient->createOrUpdateUser($userData);
    // Return the result of the operation// return $result;}
}
;
// Initialize Engagespot client
$engagespot = new EngagespotClient($apiKey, $apiSecret);
// Define unique identifier for the user
$userUniqueIdentifier = 'john@test.com';
// Define user profile d
$userProfile = [
'email' => 'john@test.com',
'firstName' => 'John',
'phoneNumber' => '+910000000000'
];
// Create or update user in Engagespot
$result = $engagespot->createOrUpdateUser($userUniqueIdentifier, $userProfile);
return $result;
}
/**
* Send a notification to a specific Engagespot user.
*
* This method will send a notification to a specific user in Engagespot.
*
* @return array The result of the send operation.
*/
public function sendNotificationToEngagespotUser()
{
// Retrieve API key and secret from environment variables
$apiKey = env("X_ENGAGESPOT_API_KEY");
$apiSecret = env("X_ENGAGESPOT_API_SECRET");
// Initialize Engagespot client
$engagespot = new EngagespotClient($apiKey, $apiSecret);
// Define unique identifier for the user
$userUniqueIdentifier = 'john@test.com';
// Define notification data
$notificationData = [
'notification' => [
'title' => 'This is an example message!',
],
'sendTo' => [
'recipients' => [$userUniqueIdentifier],
],
];
// Send notification to user
return $engagespot->send($notificationData);
}
/**
* Generate a user token for a specific Engagespot user.
*
* This method will generate a user token for a specific user in Engagespot.
*
* @return string The generated user token.
*/
public function generateUserToken()
{
// Retrieve API key, secret, and signing key from environment variables
$apiKey = env("X_ENGAGESPOT_API_KEY");
$apiSecret = env("X_ENGAGESPOT_API_SECRET");
$signingKey = env("X_ENGAGESPOT_SIGNING_KEY");
// Initialize Engagespot client
$engagespot = new EngagespotClient($apiKey, $apiSecret);
// Set signing key in Engagespot client configuration
$engagespot->setConfig('signingKey', $signingKey);
// Define unique identifier for the user
$userUniqueIdentifier = 'john@test.com';
// Generate user token
return $engagespot->generateUserToken($userUniqueIdentifier);
}

Frontend Integration (Vue.js)

1. Create a Vue component for notifications:

vue create NotificationComponent

2. NotificationComponent.vue .

Step 3: Define Routes Add routes in routes/api.php to handle Engagespot operation


use App\Http\Controllers\EngagespotController;
Route::post('/create-user', [EngagespotController::class, 'createEngagespotUser']);
Route::post('/send-notification', [EngagespotController::class,
'sendNotificationToEngagespotUser']);
Route::post('/generate-token', [EngagespotController::class, 'generateUserToken']

Step 4: Configure Engagespot API Keys Set your Engagespot API keys as environment variables in your Laravel .env fil

X_ENGAGESPOT_API_KEY=your_api_key
X_ENGAGESPOT_API_SECRET=your_api_secret
X_ENGAGESPOT_SIGNING_KEY=your_signing_k

Step 5: Run Your Application

Compile your assets and start the Laravel server:

npm run dev
php artisan serve

Step 6: Test Engagespot Integration

1. Access your application in a browser.

2. Test the Engagespot features, such as creating users, sending notifications, and generating user tokens by using postman.

3. Verify the functionality by observing the notifications rendered by the NotificationComponent.vue .

Conclusion

You've successfully set up a Laravel Vue.js project integrated with Engagespot for personalized notifications. You can now build out your application, leveraging the power of Laravel on the backend, Vue.js on the frontend, and Engagespot for user engagement. Keep exploring and enhancing your project to deliver a seamless user experience. Happy coding!

Jobin Jose

Share this post