Utilizing Product Videos in Gatsby.js Commerce

Emma Alder

Gatsby.js is a front-end web technology that helps streamline making fast, search-engine friendly and secure websites. It does that using modern technologies like React and GraphQL.

This post explores using product videos on an e-commerce store to engage and convert users.

Care should be taken as videos not optimized can affect the performance and speed of the website.

We’ll utilize the Cloudinary video player, which helps us render an optimized video.

At the end of this tutorial, we’ll gain a solid understanding of how to render a product video using the URL of a video stored on Cloudinary. A user should be able to carry out video transformations on the video player if they choose.

Sandbox

We completed the entire project on Codesandbox, and you can fork it to run the code.

Prerequisites and Installation

This article assumes that you have a basic knowledge of JavaScript, and React.js is required. However, understanding Gatsby.js is not a necessity.

We’ll use the gatsby-starter-ecommerce starter to scaffold the front-end of an e-commerce site from the command line. First, we install Gatsby globally and create a new project in our desired directory with the URL of the starter template. Gatsby ships starters to help scaffold projects quickly.

The Gatsby CLI is available via npm, and we install it using the command:

1npm install -g gatsby-cli

We create a new project titled “gatsby-starter-ecommerce” from an e-commerce starter using the command:

1gatsby new gatsby-starter-ecommerce https://github.com/parmsang/gatsby-starter-ecommerce

The above command installs Gatsby and creates a new project titled “gatsby-starter-ecommerce”.

Cloudinary is a cloud-based media management service that handles all digital media stored, transformed, and optimized for a great media experience. We’ll use the Video component shipped with Cloudinary’s React.js library, cloudinary-react, to render and display the video.

We need to install the dependencies in our project using the CLI command.

1npm install cloudinary-react

To start the development server on localhost:8000, we run the app using the command:

1npm run develop

Product video creation

We create a new directory in the src/components directory called VideoPlayer, and in there, we create a file called index.js that would contain all the components and styles we need to run our video player.

1// src/components/VideoPlayer/index.js
2 import React, {useState, useEffect, memo} from 'react'
3 import {Video, CloudinaryContext} from 'cloudinary-react'

We create a function component for the video player. The component will return a Cloudinary video wrapped around a Cloudinary Context component. Cloudinary Context provides a container with shared data accessible by all child Cloudinary components.

1const ProductVideo = ({video = 'video-ecommerce/tomato'}) => {
2 return (
3 <CloudinaryContext cloudName="codeg0d">
4 <Video
5 publicId={video}
6 controls
7 width="100%"
8 />
9 </CloudinaryContext>
10 )
11 }
12
13 export default ProductVideo

From the Cloudinary Context, we provided a cloud name. To obtain a cloud name, you need to register a new account on Cloudinary to get the cloud name value from your dashboard.

Rendering the video player

With the completed steps above, we render a ProductVideo component into the ProductAttributes component, in src/components/index.js. Next, we update the code component to include the video player.

1import React from 'react'
2 import {Header, Divider, Table} from 'semantic-ui-react'
3 import ProductVideo from '../VideoPlayer/index'
4
5 export default ({description, material, max_watt, bulb_qty, finish, bulb}) => (
6 <div>
7 <Header as="h3">About this product</Header>
8 <p>{description}</p>
9 <Divider />
10 <Table celled>
11 {/*table information goes in here*/}
12 </Table>
13 <Divider />
14 <ProductVideo />
15 </div>
16 )

Video transformation

In this post, we stated that Cloudinary is cloud-based, and it offers the possibility for transformation of videos using the Transformation URL API Reference, which include all transformation parameters.

We can apply transformations to the video rendered in the ProductVideo component.

1const ProductVideo = ({video = 'video-ecommerce/tomato'}) => {
2 return (
3 <CloudinaryContext cloudName="codeg0d">
4 <Video
5 publicId={video}
6 controls
7 width="100%"
8 sourceTransformation={{
9 ogv: {
10 aspect_ratio: '3:2',
11 },
12 webm: {
13 aspectRatio: '1:1',
14 },
15 }}
16 sourceTypes={['webm', 'ogv', 'mp4']}
17 />
18 </CloudinaryContext>
19 )
20 }

In the above code block, we defined the source types and source transformation parameters for our video.

The sourceTransformation parameter specifies the aspect ratio for each video format and determines how the asset (video) is adjusted to new dimensions. Finally, the sourceTypes parameter is an ordered array of the video source types to include in the HTML5 tag, where the type is mapped to the mime type.

There are multiple other parameters we can include based on our preference and what we want to achieve.

With these, we have the product page looking like this. Spot the video after the product attributes.

Summary

The use of product videos in your e-commerce store can improve customers’ trust and boost sales. With Cloudinary, this is possible as we can optimize and transform product videos for the best user and media experience.

Resources

You may find these resources helpful.

Emma Alder

Technical Writer at Hackmamba.io

Technical writer at Hackmamba.io