On GitHub, you can take a look at the source code, or read through the existing issues to see if somebody else had a similar problem. If you're still stuck, feel free to create a pull request or open an issue yourself!
Use the Send Abandoned Cart Reminders with SendGrid Firebase extension to automate sending an email reminder to users about items they left in their shopping cart.
This extension will watch the documents added to a specified Cloud Firestore collection. For each document the extension records the last updated time of the document. Then, when the last updated time goes beyond a configurable threshold, the information in the document is copied to a new collection which triggers an email using the SendGrid API. The information in the document is sent as the template data for a dynamic transactional email.
This extension uses the following flow to send Abandoned Cart Reminders:
You create a new document in the cartFirebase
collection. The document should either have the same ID as your customer in Firebase Auth or it should have a property userId
which is the same as your customer in Firebase Auth. This document represents the Customer's cart, and typically contains an array property with items
, for example.
When you create the cart document or update properties on the cart, the extension will update a property called lastUpdated
, giving the extension a sense of whether the cart is abandoned.
A function will regularly check the collection of cart documents to determine if the cart has been abandoned
If the lastUpdated
metadata property indicates that the cart was abandoned (as determined by an interval you can define during setup), it will add a document to an email
collection. This tells the extension that it needs to email the customer, prompting them to return to their cart
Otherwise, the extension determines that the cart isn't abandoned and will ignore it until the document is removed (indicating that the customer has checked out)
Before installing this extension, make sure:
This extension requires a composite Firestore index. You can add the index in the Firebase console or by the command line.
Go to the Cloud Firestore section of the Firebase console
Go to the Indexes tab and click Add Index
Enter the collection name for your cart collection
Add the following fields to the index:
metadata.emailSent
- Ascendingmetadata.error
- Ascendingmetadata.lastUpdated
- AscendingSet the Query scopes to Collection
Click Create
In your Firebase project, open your index configuration file, with default filename firestore.indexes.json
Add the following object to the indexes
array:
1{2"collectionGroup": "cart",3"queryScope": "COLLECTION",4"fields": [5{ "fieldPath": "metadata.emailSent", "order": "ASCENDING" },6{ "fieldPath": "metadata.error", "order": "ASCENDING" },7{ "fieldPath": "metadata.lastUpdated", "order": "ASCENDING" }8]9}
The collectionGroup
name should be the collection name for your cart collection.
Deploy your index configuration with the firebase deploy
command. If you only want to deploy indexes, add the --only firestore:indexes
flag.
Due to the nature of the extension, it's difficult to see the full effects of this extension immediately. Here's what you can do:
Go to your Cloud Firestore dashboard in the Firebase console.
If it doesn't already exist, create the cart collection you specified during installation: ${param:CART_COLLECTION}
Add a document with an items
field containing some products:
1{2"items": [{ "name": "Test product 1", "price": "$9.99" }]3}
In a few seconds, you will see a metadata
field appear in the document. The field will contain a lastUpdated
property that will be set to the current time
If you update any properties outside the metadata
property, you will see the metadata.lastUpdated
property update shortly after
Once ${param:ABANDONED_TIMEOUT}
minutes have passed, the cart will be updated again and a new document will be placed in the ${param:EMAIL_COLLECTION}
collection
In a few seconds, you will see a delivery
field appear on the new document in the ${param:EMAIL_COLLECTION}
collection. The field will update as the extension processes the message and sends it via the SendGrid API
Using the extension begins with tracking your customers' carts. A shopping cart should be represented as a single document in the collection per cart. How you store items in the document is up to you. Typically, a cart will include an array property called items
, which contains information about each of the items in the cart.
The document should also have a reference to a Firebase Authentication User. Either the cart document ID should match the user ID or there should be a userId
property on the document. When you create the cart document or update properties on the cart document, the extension will update a metadata.lastUpdated
timestamp.
You can configure the period with which the extension checks the cart by updating the CART_CHECK_INTERVAL
, which should be declared in extension.yaml
using cron.yaml syntax.
A cart document must fulfill the following conditions before it triggers an abandoned cart reminder:
metadata.lastUpdated
timestamp should be older than the configurable ABANDONED_TIMEOUT
time in minutesmetadata.emailSent
Boolean property should be false
metadata.error
propertyIf all of these conditions are met, then the extension will attempt to load the user data using the userId
property or the document's ID.
If the user doesn't have an email address, an error will be recorded.
If the user has an email address then a document will be created in the EMAIL_COLLECTION
. The document will include the user email and a property for dynamicTemplateData
consisting of the contents of the user's cart and a user
property including the user's email
and displayName
if present. This dynamicTemplateData
is used to fill in the fields in a SendGrid dynamic email template.
When a document is added to the EMAIL_COLLECTION
the contents are queued up to be emailed using the Twilio SendGrid API. All the information from the cart document is added as dynamic template data for the email. You can configure a DEFAULT_TEMPLATE_ID
which is the ID of a SendGrid dynamic template.
You can create dynamic transactional templates in the SendGrid dashboard. SendGrid Templates use Handlebars to render dynamic data into the email.
You also need to configure your DEFAULT_FROM
to be an email address that you have verified with SendGrid as either a single sender or via domain authentication. You can also set a DEFAULT_REPLY_TO
email.
Furthermore, you can send an email at any time by adding a document to the EMAIL_COLLECTION
with a to
email address and a dynamicTemplateData
property.
1admin.firestore().collection('cart_emails').add({2to: 'example@example.com',3dynamicTemplateData: {4name: "Example"5}6});
On GitHub, you can take a look at the source code, or read through the existing issues to see if somebody else had a similar problem. If you're still stuck, feel free to create a pull request or open an issue yourself!