Build an Ecommerce App with Personalization using Angular and Cosmic JS


* This article will assume some basic knowledge of Angular so it can focus on the specific task at hand. Feel free to ask me about any specifics on the implementation you may find unclear

TL; DR

Angular Ecommerce Personalization

Download the codebase

What are we going to build?

This site will be a very simplistic representation of an ecommerce website and its purpose is to show how we can offer a customized experience for everyone. Our data will be stored and served by Cosmic JS and we will use Angular for our Front-End.

How will it work?

Our storefront will be just a product listing page, showing all products stored in Cosmic JS, randomly sorted. The products will belong to a number of categories, and each product will show 3 buttons to mock the following behaviors:

  • view -> value: 1
  • add to cart -> value: 2
  • buy -> value: 3

These will represent the different degrees of interest.

The user will interact with the site and its interests will be stored in its profile, increasing the corresponding value of each product's categories.

A full interaction would be something like: the user clicks on "buy" a "swimwear" product, increasing its profile score for products belonging to "swimwear". The page then will sort the page showing more prominently the products belonging to "swimwear".

Preparing our bucket

The first thing we'll do is prepare our Cosmic JS bucket. We have to create an empty bucket and three object types:

  • Categories
  • Products
  • Users

Each user will store a sessionID and a JSON object for interests. Each product will have a price, a collection of categories and an image. Categories won't need any metadata, we will just use its title.

If you are as lazy as I am, you can always replicate the demo bucket by installing the app.

Preparing the site

The first thing we need to do is create a new Angular site (I always recommend using the CLI for that). Now, let's create a HTTP interceptor that will handle the authentication against the Cosmic JS API, so we don't need to add this in every call. It will append the read_key parameter for the GET requests and the write_key for everything else.

The product listing will be our only page, so after creating the component, our routes should look like this:

The product listing page

We need to show the products, so let's define our model:

*We'll talk about the Category there later.

Now let's begin populating our service with methods. First of all, we need a getProducts() method:

*We are caching the response, our products won't change that often and we are going to call this method A LOT...

The component itself will be quite simple. For now, it will only have the call two calls to our service:

And this is the HTML:

Have you noticed that app-actions component? It will take care of the actions the user will perform over each product. But let's talk about the user before that...

Everything about the user

Our user model will be the most "complex" of all. As we will be creating users from the site and retrieving them from Cosmic JS, we need a more flexible constructor. We also added methods to build a payload object for posting to Cosmic JS and another one for managing the interests.

We won't build a proper authentication journey for our example. Each user will have a sessionID on the browser's local storage, and we will use that to create and identify users. We'll keep the user instance on session storage for convenience. In order to keep things tidy, let's create a user service:

The init() method will be used on our app.component.ts:

We will also need a way of setting and getting the user, let's extend our cosmic.service with the following methods:

Also note that we created an observable user$ that emits every time we set (or update) the user in session, you already saw the subscription on the product listing. More on this at the end of the article.

Back to the actions.component. It is a set of three buttons, as follows:

And the methods look like this:

When updating the interests, we also refresh the user in session and we also update the user in Cosmic JS, we never know when or how the user will leave the site! Let's go back to our cosmic.service and add this method:

Wrapping everything up

We have now a way to show products, create users and their interests, update them and store them. That's it... almost. How does the product listing page change based on all this? There is one last thing we haven't go through yet: the customSort pipe on product-listing.component. Here is how it looks like:

This pipe will take the product list and order it by weight, based on the user's interests provided via arguments...

... And that's the magic: the product listing page is subscribed to the changes in the session user, so every time the user performs an action, the user changes and the sorting re-executes with the changes.

That's all, I hope this would have demonstrated how to offer a customized experience for your users with the help of Angular and Cosmic JS.

Was this article helpful?

Thank you! Your input helps us improve our articles and resources. If you have any other questions or comments, please contact support.

You may also like


This article shows you how to create a blazing fast Gridsome blog using Cosmic JS source plugin. Let’s get started.
Salesforce is the world’s leading enterprise CRM application. Zapier gives people internet superpowers by allowing them to connect and automate the apps they use. Using Zapier to connect your Cosmic-powered content with Salesforce is easy and requires zero coding.
For simple shopping websites, going with full-fledged solutions like Shopify or Woocommerce can be a total overkill. In this tutorial, we're going to build an e-commerce website using Nuxt and Cosmic JS.
This application was mainly inspired by Offerup, Letgo, and Craigslist. Like Craigslist, LetGo allows you to buy and sell items locally, which means you don’t have to deal with the hassles of shipping like you would on eBay or Amazon. With 30 million users, it is more popular than OfferUp...
The Cosmic JS team was honored to to speak at Toyota Connected for the Ember.js Dallas Meetup this week. Cosmic JS co-founder Tony Spiro presented "Building Modern Apps with API Services" to illustrate the growing trend of offloading core dev tasks onto best-in-class API services.
All of your images you upload to Cosmic JS are already transformed to imgix URLs, a powerful real-time image processing and CDN delivery solution. Let's take these images to the next level by optimizing image formats, compressing, intelligently cropping, & more!