Add the sales tracking script with authorization:
Follow the below steps to add sale tracking script with authorization.
- Server Side coding for JWT token generation:
To integrate the Sale Track script into the client’s website, first, the client will need to generate tokens at the end using JWT token generation functionality. For this, the client will need to implement the following JWT token generation logic on their server side:
a1. Initialize the following dynamic parameters:
- order_id: Pass order id placed by the specific user.
- sale_amount: Pass specific sale amount, without currency symbol.
- email_id: Pass a user’s email who has placed an order.
- site_id: Pass the specific site id provided by Annex Cloud (refer Defaults section)
- secret_key: Pass the secret key provided by Annex Cloud (refer Defaults section), which will be used to generate to hash value for token generation.
a2. Initialize the timestamp = server time + 120 sec, as token expiration period. While passing this value to Annex Cloud, make sure it should be in UTC format.
a3. Initialize the following headers:
- typ =JWT
- alg =HS256
- exp =timestamp, which we have set in the previous step
a4. After initialization of this header data, encode it in JSON format.
a5. After that, they encode the (JSON encoded) headers in base64 format.
a6. Now, initialize the payload, with the following parameters:
- site_id
- email_id
- order_id
- sale_amount
a7. After initialization of this payload, encode it in JSON format.
a8. After that, encode the (JSON encoded) payload in base64 format.
a9. Use the HMAC method to generate hash values, using the following parameters:
- sha256 algorithm
- Concatenate header data & payload data using dot (.) e.g. $header.$payload
- secret_key (use the secret key provided by Annex Cloud, refer to Defaults section).
- raw_output
a.10. Encode this generated hashcode in base64 format.
a.11. At the end, concatenate the header data, payload data & generated hash value using dot (.)
e.g. $header.$payload.$hashvalue. This is the JWT token.
Now, the client will need to pass this JWT token to the following sale tracking script as bearer token.
Refer to the following sample server side code for JWT token generation:
Sample PHP Code:
<?php
$order_id = 'TESTORDER10014';
$sale_amount = 40.49;
$email_id = 'stevemartin144.sa@gmail.com';
$site_id = 9991650;
//Secret key to generate hash
$secret_key = '1529720a36dec2260c3e8a8e63002c4b';
//Make sure this is UTC
$timestamp = time() + 120;
//Headers
$header = [
'typ' => 'JWT',
'alg' => 'HS256',
'exp' => $timestamp
];
//Encode header data in JSON
$header = json_encode($header);
//Encode header data in base64
$header = base64_encode($header);
//Payload
$payload = [
"site_id" => $site_id,
"email_id" => $email_id,
"order_id" => $order_id,
"sale_amount" => $sale_amount
];
//Encode payload data in JSON
$payload = json_encode($payload);
//Encode payload data in base64
$payload = base64_encode($payload);
// Generates a keyed hash value using the HMAC method
$signature = hash_hmac('sha256','$header.$payload', $secret_key, true);
//base64 encode the signature
$signature = base64_encode($signature);
//concatenating the header, the payload and the signature to obtain the JWT token
$token = "$header.$payload.$signature";
?>- Add the following script on the order confirmation page, which will be used to track the sale generated through the Annex Cloud modules.
<script>
var productDetailsObj = {
'1' : {
'id' : 'PASS_PRODUCT_ID',
'qty': PASS_PRODUCT_QUANTITY,
'price': 'PASS_PRODUCT_PRICE',
'product_category_id': 'PASS_PRODUCT_CATEGORY_ID',
'estimated_ship_date': 'PASS_ESTIMATED_SHIP_DATE',
},
'2' : {
'id' : 'PASS_PRODUCT_ID',
'qty': PASS_PRODUCT_QUANTITY,
'price': 'PASS_PRODUCT_PRICE',
'product_category_id': 'PASS_PRODUCT_CATEGORY_ID',
'estimated_ship_date': 'PASS_ESTIMATED_SHIP_DATE',
},
};
var couponDetails = 'couponDetails';
var productsDetails = JSON.stringify(productDetailsObj);
var site_id='PASS_SITE_ID';
var order_id ='PASS_ORDER_ID';
var sale_amount ='PASS_SALE_AMOUNT';
var user_email='PASS_USER_EMAIL';
//Build URL params for request
var sa_params = 'site_id='+site_id+'&order_id='+order_id+'&sale_amount='+sale_amount+'&email_id='+user_email+'&exclude_products='+ productsDetails+'&coupon='+ couponDetails;
//Function for request
function createCORSRequest(url, data){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
// XHR has 'withCredentials' property only if it supports CORS
xhr.open("POST", url, true);
xhr.withCredentials = true;
} else {
xhr = null;
}
if(xhr !== null){
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Authorization', 'Bearer '+'PASS_JWT_BEARER_TOKEN');
xhr.onload = function(){};
xhr.send(data);
}
return xhr;
}
createCORSRequest("https://c.socialannex.com/c-sale-track?origin=PASS_CLIENTS_DOMAIN",sa_params);
</script>To the above sale tracking code, pass the following values:
- PASS_PRODUCT_ID: Pass the specific product id purchased by the user.
- PASS_PRODUCT_QUANTITY: Pass the specific product quantity purchased by the user.
- PASS_PRODUCT_PRICE: Pass the specific product price, without currency symbol.
- PASS_PRODUCT_CATEGORY_ID: Pass the specific product category.
- PASS_ESTIMATED_SHIP_DATE: Pass the specific product estimated ship date.
-
couponDetails: Pass the coupon code(s) used by the user. Following are the examples to pass coupon codes
var couponDetails = '["CodeA","CodeB"]'; //For Multiple Coupons
var couponDetails = "CodeA"; //For Single Coupon
var couponDetails = ""; //For No Coupon - PASS_SITE_ID: Pass the specific site id provided by Annex Cloud.
- PASS_ORDER_ID: Pass the specific order id.
- PASS_SALE_AMOUNT: Pass the total sale amount, without currency symbol.
- PASS_USER_EMAIL: Pass specific user’s email address.
- PASS_JWT_BEARER_TOKEN: Pass the JWT bearer token, which the client has generated in JWT token generation step.
-
PASS_CLIENTS_DOMAIN: Pass the client’s domain, from which the specific user has placed an order
e.g. https://perf.store.hp.com
Note: Please provide allowable domains to restrict CORS request.