DEV Community

Jean-Rémy Duboc
Jean-Rémy Duboc

Posted on • Originally published at codaille.com

Analytics Integration with TypeScript, React and Styled Components - Part 1: Setting up the Tools

Image description

In this series of tutorials, we are going to create a simple web applications using 3 technologies:

We will learn how to track user actions in the app using Google Analytics 4. We'll also use the Web Vitals library to measure and send the app's performance data to GA4.

The aim is to dive into the basics of modern front-end workflows, and how they integrate with Google Analytics.

Prerequisites

In this first part of the tutorial, we'll learn how to set up the basic tools for this application.

To do this tutorial you'll need:

  • your command line tool of choice
  • node, npm and npx installed on your computer. At the time of writing I'm using node 16.11.0, npx 8.0.0 and nvm 0.39.1.

I'm also using VSCode and the Git bash command line, but you can use whatever command line and code editor you prefer.

Creating a new React app with TypeScript

The first step is to use create-react-app to generate a simple application as a starting point. create-react-app spares us the work of configuring several build tools, and makes our life easier. Among many others, it will automatically set up Webpack and Babel(configured to compile TypeScript), as well as web-vitals.
All of this in a single command!

Go to the folder where you want you application to live, then type the below in your command line:

npx create-react-app my-react-ts-app --template typescript
Enter fullscreen mode Exit fullscreen mode

That's it ! Your application now lives in a new folder called /my-react-ts-app. You'll notice a few configuration files already in place such as package.json and tsconfig.json. You also have the source files for the application itself already in place in the /src folder.

Start your application by typing:

npm start
Enter fullscreen mode Exit fullscreen mode

Install Styled Components for TypeScript

The next step is to add Styled Components to our apps. Start by installing the styled-components library:

npm i styled-components
Enter fullscreen mode Exit fullscreen mode

Then, we install the styled-components types for TypeScript:

npm i --save-dev @types/styled-components
Enter fullscreen mode Exit fullscreen mode

Read more about these types at: https://github.com/DefinitelyTyped/DefinitelyTyped.

We've just installed the styled-components declaration file. Ready to go!

Add a new styled component

Now we'll create a very basic style component, with conditional styling. Create a new file called Heading.tsx with the following content:

import styled from 'styled-components';

export const Heading = styled.h1<{ active: boolean }>`
  color: ${props => (props.active ? 'red' : 'blue')};
`;

export default Heading;
Enter fullscreen mode Exit fullscreen mode

This will create a new <h1> component class that takes a boolean prop called active, If active is set to true, we set header's color to blue, otherwise we'll paint it red.

Let's test it: go to App.tsx and copy the below content:

import React from 'react';
import logo from './logo.svg';
import './App.css';
import Heading from './Heading';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <Heading active={false}>My Heading</Heading>
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.tsx</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer">
          Learn React
        </a>
      </header>
    </div>
  );
}


export default App;
Enter fullscreen mode Exit fullscreen mode

We've imported the Heading component class, and inserted a heading with active set to false.
Here's what the result should look like:

Image description

Well done! You're ready to build your React application in TypeScript using Styled Components!

Top comments (0)