Integrate the PayPal Shopping SDK
Last updated: Feb 27th, 8:40am
Important: The PayPal Shopping SDK is available to select partners and merchants for approved use cases.
Set up your development environment
Before you can integrate the PayPal Shopping product suite, you must set up your development environment. After you get a token that lets you access protected REST API resources, you create sandbox accounts to test your web and mobile apps. For details, see Get started.
Then, return to this page to integrate the PayPal Shopping product suite.
Note: You must be in a live environment to obtain a production client-id.
Register your account
Use this URL to register your account with your client_id:
1https://www.paypal.com/cartrecovery/signup?clientId= <insert_client_id>
Install and instantiate
This script sends an email to remind the user about their cart after information remains in their cart for about five minutes. If the user's browser contains a PayPal cookie, the email sends automatically. If their browser does not contain a PayPal cookie, a dialog box displays and asks the user if they want a copy of their cart sent to their email. The email sends at the top of the hour.
Install script
Set up your website to send the customer's shopping events to PayPal. Install and instantiate the SDK by adding the following code to the HEAD section of every page where you want to use it.
- Merchant
- Partners
1<script src="https://www.paypal.com/sdk/js?client-id=<insert_production_client_ID>¤cy=USD&components=buttons,tracker,messaging"></script>
| Variable | Description |
|---|---|
client-id | For merchants, this is the production client id. For partners, this is the partner client id. |
merchant-id | For partners only. |
Initialize script
After loading the script into the pages, initialize the script. Depending on how your application manages JavaScript scopes, you may want to initialize the messaging object and the tracker object as shown in this example:
1var uniqueId = Date.now(); //or your unique session ID2paypal.Messaging({3 cartRecovery: {4 userId: uniqueId5 }6});7var tracker = paypal.Tracker({});8tracker.setUser({9 user: {10 id: uniqueId,11 name: "example_name",12 email: "example_name@example.com"13 }14});
Track information
Use the tracker object to track information added or removed from the cart. In the script, you must include an id, title, url and imgUrl. You can include other fields from your item's JSON such as weight, tax, or barcodes.
| Field | Description |
|---|---|
id | The item ID |
title | The subject line in the email to the customer |
url | The link to the item in the customer's cart |
imgUrl | The link to the image of the item in the customer's cart |
This script tracks information added to a cart:
1tracker.addToCart({cartId, items: [{2 id: itemId,3 title: 'Product Title',4 url: 'https://example.com/myitemid',5 imgUrl: 'https://example.com/images/items/myitemid.jpg',6 price: '1.00'7}], total: total.toFixed(2)8});
This script tracks information removed from a cart:
1tracker.removeFromCart({cartId, items: [{2 id: itemId,3 title: 'Product Title',4 url: 'https://example.com/myitemid',5 imgUrl: 'https://example.com/images/items/myitemid.jpg',6 price: '1.00'7}], total: total.toFixed(2)8});
This script deletes the record when the customer completes checkout:
1tracker.purchase({2 cartId3});
Track shopper activity on your site
After you install and instantiate the SDK on your pages, you can track shoppers. The PayPal Shopping SDK uses shopping events to identify shoppers and understand their behavior on your site. Using events, PayPal can build a representation of a shopper's cart and keep track of whether the shopper makes a purchase.
PayPal Tracker is JavaScript code that tracks visitor activity on your website. Use the following JavaScript functions from the PayPal Shopping SDK to implement PayPal Tracker.
Initialize
Instantiate the paypal.Tracker global variable before using it to send events.
paypal.Tracker.(params)
| Name | Type | Required | Description |
|---|---|---|---|
user | object | no | Contains the shopper's data. |
user.id | string | no | The id of the shopper in your system. You can use the shopper's email if you don't maintain user ids. |
user.email | string | no | The shopper's email. |
user.name | string | no | The shopper's name as you want it to appear in your reports and emails. |
paypal.Tracker (params) Example
1paypal.Tracker({2 user: {3 id: '3837394700',4 email: 'john@doe.com',5 firstName: 'John',6 lastName: 'Doe'7 }8})
Set user
If you know who the visitor is, use the setUser function. All events emitted on the page are attributed to this visitor.
Note: By default, the Tracker tracks events against an anonymous user and assigns them an auto-generated ID. If you later make a setUser() call, the Tracker includes those previously-anonymous events for that user.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
user | object | no | Contains the shopper data. |
user.id | string | no | The id of the shopper in your system. You can use the shopper's email if you don't maintain user ids. |
user.email | string | no | The shopper's email. |
user.firstName | string | no | The shopper's first name, as you want it to appear in your reports. |
user.lastName | string | no | The shopper's last name, as you want it to appear in your reports. |
paypal.Tracker.setUser(params) Example
1/*2 * Example of an event emitted when a known user visits your website.3 */4paypal.Tracker.setUser({5 user: {6 id: '3837394700',7 email: 'john@doe.com',8 firstName: 'John',9 lastName: 'Doe'10 }11})
paypal.Tracker.view(params)
Use this function to report on what page is being viewed. This function helps you build an audience in PayPal for targeting your visitors with PayPal's marketing products. PayPal recommends you use relative URIs for this.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
page | string | yes | The URI of the page. Can be absolute or relative. |
title | string | no | The title of the page. Defaults to the site's title if not given. |
paypal.Tracker.view(params) Example
1/*2 * Example of an event emitted when a visitor lands on a page3 */4 paypal.Tracker.view({5 xspage: 'carts/1000',6 title: 'Cart Page'7})
Carts
The following functions help you track cart-related events. To run an effective targeted campaign, cart-related functions are crucial.
Note: PayPal recommends you send in as many of the optional fields as possible. For products like Cart Recovery, PayPal attempts to augment missing information from other data sources such as your product catalog. If PayPal cannot augment the information, PayPal uses these optional fields to power Cart Recovery.
paypal.Tracker.addToCart(params)
Triggers when a user adds items to a cart.
| Name | Type | Required | Description |
|---|---|---|---|
cartId | string | no | The shopping cart's identifier in your system. If you do not supply an ID, PayPal generates a random one. |
total | string | no | The total price of the cart. If none is provided, PayPal totals the value of all the items in the cart. |
currencyCode | string | no | Uses ISO 4217 Currency Code. Defaults to usd if not present. |
items | array | yes | An array of item objects. |
items[n].id | array | yes | The ID of the item. Use the SKU if available. |
items[n].title | array | no | The name of the item. |
items[n].url | array | no | The URL of the item. If you use relative URLs, PayPal infers the absolute URL based on the site's URL. |
items[n].description | no | yes | Description of the item. |
items[n].imageUrl | string | no | The primary image's absolute URL. |
items[n].otherImages | string | no | Comma-separated urls of other images. |
items[n].keywords | string | no | Comma-separated search keywords. |
items[n].price | string | no | The price of the item. |
items[n].quantity | string | no | The number of units of this item. |
paypal.Tracker.addToCart(params) Example
1/*2 * Example of an event emitted when an item is added to cart3 */4paypal.Tracker.addToCart({5 cartId: '3938349999',6 items: [{7 id: '01120000',8 name: 'Blue Jeans',9 url: '/products/blue-jeans',10 description: 'The original zip fly. First created in 2000, the Regular Fit Jeans',11 imgUrl: 'https://myshop.com/products/blue-jeans.jpg'12 otherImages: 'https://myshop.com/products/blue-jeans-1.jpg,https://myshop.com/products/blue-jeans-2.jpg',13 keywords: 'jeans,blue,relax',14 price: '34.00'15 }],16 total: '34.00',17 currencyCode: 'usd'18})
paypal.Tracker.setCart(params)
This function sets the cart for the current visitor. It takes the same parameters as addToCart.
Note: Using paypal.Tracker.setCart will overwrite the existing cart.
paypal.Tracker.removeFromCart(params)
This function removes items from the current cart. It takes the same parameters as addToCart.
Note: Using paypal.Tracker.removeFromCart modifies the existing cart.
paypal.Tracker.purchase(params)
This function marks a specific cart as purchased. If you do not provide the cart ID, PayPal uses a last known un-purchased cart ID.
| Name | Type | Required | Description |
|---|---|---|---|
cartId | string | no | The shopping cart's identifier in your system. If you do not supply an ID, PayPal generates a random one. |
1/*2 * Example of an event emitted when an a cart is purchased3 */4paypal.Tracker.purchase({5 cartId: '3938349999'6})
Send shopping cart reminders
After you add the Shopping SDK to your site, PayPal can detect when a shopper begins a PayPal transaction but does not complete it. In this instance, PayPal automatically sends a cart reminder email to shoppers who have not completed a purchase. Sellers can track the performance of these emails from the client dashboard provided in the Onboard your seller section.
Rebuild abandoned shopping carts
When you notice a shopper abandons a cart, you can recover and rebuild it. When a shopper clicks a link in a cart recovery email, PayPal passes the cart ID as a parameter to this link. You can use this cart ID to rebuild the shopper's cart.
Onboard your seller
In order to capture the seller's information and opt-in to PayPal Shopping, you must create an onboarding flow for your seller.
- Present your seller with a UI element, such as a checkbox, to enable PayPal Shopping in your platform.
- When they have checked the box, present a link for the seller to Manage PayPal Shopping that links to
https://www.paypal.com/cartrecovery/signup?partnerUrl=RETURN_URL&clientId=CLIENT_ID. ReplaceRETURN_URLwith the URL you want PayPal to redirect the seller to when they're done setting up PayPal Shopping. ReplaceCLIENT_IDwith the seller's PayPal Client ID. This is the PayPal Shopping dashboard where the seller can manage configuration details.
Once the seller has enabled PayPal Shopping, the link goes to their Performance Dashboard, where they can monitor the performance of their Cart Recovery emails.
Tip: Use the seller's opt-in status to determine whether or not to serve the PayPal Shopping SDK and associated code on their store's web pages.