Mastering Embedded Analytics: A Getting Started Guide 📊
Tags: webdev, javascript, programming, opensource
Published: 2024-06-11 09:12:13

## **TL;DR**

This article will walk you through all the information you need about embedded analytics and [**Latitude**](https://tools.latitude.so/), including how to use Latitude to visualize your data, with a detailed guide.


![Open glasses meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj1nxle54ixegi27ovzg.png)

## **Overview of Embedded Analytics**

At this point, you may want to know what **embedded analytics** is. It refers to integrating data visualization into software or a web application. Instead of viewing data as numbers or as disorganized, there are different ways to display data in an organized way and embed it in an application.

Modern technologies like charting libraries, web components, APIs, and SDKs have made data visualization seamless. An alternative to visualizing your data could be using an underlying technology, but it could be challenging to achieve compared to emerging technologies such as the ones mentioned earlier.

Modern charting tools provide users with pre-built components that make them easy to work with. While using these tools, you only need to provide raw data to work with in most cases. This makes it easy to include your data visualization in your application.


![are you ready gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxiidgydos0owp34lwjm.gif)

**ICYMI**: We published an article a couple of weeks ago on the [**best chart libraries to use in 2024**](https://blog.latitude.so/7-best-chart-libraries-developers-2024/). It's best to read that article if you are looking for a chart library to work with. I've included in the article comprehensive details about the best charting libraries to work with according to your needs.

By the way, I'm a part of Latitude's team, and it would mean a lot if you could check out our open-source framework for embedded analytics and [**give us a star on GitHub**](https://github.com/latitude-dev/latitude).

We're working to provide a better developer experience, and all support is appreciated!

[**Star Latitude on GitHub ⭐️**](https://github.com/latitude-dev/latitude)

Okay, now - let's get back at it! 😉

## **Key Concepts of Embedded Analytics**

* **Real-time data access**: Embedded analytics often provide access to real-time data. This lets users view analytics based on the most current data available. While doing this, you must know that this is a dynamic way of rendering analytics.
    
* **Customization**: Most embedded analytics tools come with various pre-built components. You can customize the analytics tailored to your needs with the help of pre-built themes or custom styling. These tools also let you include features you want in your analytics. With the help of customization, you can display your analytics with the features or style you want to embed.
    
* **Integration with databases and host applications**: Embedded analytics tools let you work directly with databases and host applications as data sources. While data could be rendered dynamically, many of these tools render data differently, just as various technologies are used for database management.
    
* **Focus on User Experience**: This is the primary purpose of embedded analytics; it focuses on how data can be visualized in the best way for users. Providing great functionality in analytics helps users understand the context of the analysis.
    

There are tons of things embedded analytics could be used for. Embedding analytics works in hands with your data, but you may be wondering what its purpose is. For instance, you see a dashboard that displays the usage stats in an application as a chart—an example of embedded analytics integrated into that application. Different tools for visualizing data have various methods of sourcing data.


![let's get to work gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8loy0uuw0yxh2dzmwj4d.gif)

## **Getting Started with Latitude**

There are different tools available for embedding analytics into your application. One option is [**Latitude**](https://tools.latitude.so/), an open-source framework that allows you to build and embed interactive analytics based on your database or data warehouse.

With Latitude, you only need to write composable SQL queries with custom parameters to pull data and transform it into analytics on your front end. The framework works with **Svelte** for the front end; with the pre-built components Latitude provides, you can include your components as attributes to display your analytics.

### **How Latitude Works**

Latitude works with two major things: your **database** or **data warehouse** and your **front end**, with the help of the created database. Additionally, data can be displayed statically using CSV files. By doing this, there's no need to worry about anything apart from creating SQL queries.

Latitude isn't limited to using Svelte alone for the front end; you can also integrate Latitude into your **React** application. All you need to do after configuring the parameters is include them in the components for the data you have to display.

With Latitude, you're on the right path to embedding your analytics into your application.


![rushing gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2bnmggflwl0qefzre89.gif)

## **Prerequisites for getting started with Latitude**

* Basic knowledge of SQL.
    
* `npm >= 8.5.5` or higher installed on your local development machine.
    
* Vast knowledge of React or Svelte.
    
* Basic knowledge of CSS frameworks such as Tailwind for styling.
    

Away from the hype, Latitude is one tool you will love to try out because of its accessibility! To start with Latitude, you must install Latitude's CLI on your local machine. The command below will install Latitude's CLI globally on your local machine.

```xml
npm install -g @latitude-data/cli
```

After installing the CLI, you need to create your Latitude project. You can only configure your **queries** and **views** by creating a project in Latitude. Use the command below to create a Latitude project.

```xml
latitude start
```

This is where your actual journey of building with Latitude begins. When you run the command, you should input the project name when requested and select the type of project, whether a prebuilt project (DuckDB + CSV demo) or an empty project from scratch.

By doing that, you have your project created locally. Afterward, `cd` into the project name. After doing this, you can execute the `code .` command to work on the project from your IDE. From here, you need to understand Latitude's project structure.

---

### **Project structure**

Before working on any project, you need to understand its structure, the functionalities of each path, and the file in every path.

In Latitude, there are two significant directories: `queries` and `views`. But before going into that, here is the project structure of a Latitude project:

```xml
project-name/
├── queries/
├── views/
```

These are the only two paths to work with. In these paths, you will find files. The `queries` directory contains a configuration file, `source.yml,` and SQL queries. Although, when you create a project from scratch, you won't find any query files. You will have to create your query files.

Also, you can add your CSV file to the queries directory if you like. In this context, fundamental knowledge of SQL or other query languages is best. `queries` in Latitude can be regarded as the **data source** displayed in the embedded analytics.

For the `views` path, it controls the entire front end. You will find an HTML file in that directory that works with Svelte. In that file, you interact with your queries using parameters configured in your queries.

---

Now, we need to start working on queries. I will use [this CSV file](https://gist.githubusercontent.com/rioto9858/ff72b72b3bf5754d29dd1ebf898fc893/raw/1164a139a780b0826faef36c865da65f2d3573e0/top50MusicFrom2010-2019.csv), Top 50 Spotify Music from 2010 to 2019, as the data source in this guide. You could download the file and keep it on your `query` path.

The next thing to do is set up your queries. This is where your SQL knowledge will be beneficial. Create a file in the same `queries` directory to capture your data source. You can use the query snippet below:

```xml
select title,
       artist,
       genre,
       music_year,
       BeatsPerMinute,
from read_csv_auto('queries/spotify.csv')
order by music_year asc
```

The code snippet above shows that the only fields to extract data are the title, artist, genre, music\_year, and BeatsPerMinute fields from the CSV file. Toward the end of the snippet, the sixth line specifies the CSV file from which to pull data.

After doing this, you need another query to sum up your data. To do this, you can give the file a suitable name for an aggregated query file. In this case, I’ll name the query file `firstcomp.sql`.

```xml
select
  music_year,
  count(*) as all_titles,
from { ref('first') }
group by 1
order by 1 asc
```

In the query above, the `music_year` field from the CSV file was selected, and then the primary query file, `first.sql` , was referenced `first`. It indicates that data should be grouped differently and in ascending order.

To **verify** if your data was extracted correctly, you will have to run the query above using Latitude's CLI with this command:

```xml
latitude run second_filename
```

By doing that, this should be your result:


![result from terminal gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6s195yoa6r0skpf1w30.jpg)

If you get something like this, you're on the right path. After verifying your queries and data source, the next thing to do is work on how the data should be displayed.

Now, let's navigate to the `views/` directory to set the front end for the analytics we want to display.

Unlike the `queries/` path, where we have to work with multiple files, we will work with `index.html` alone. You can add this snippet to your HTML code:

```xml
<svelte:head>
  <title>Demo project</title>
</svelte:head>
<View class="gap-8 p-8">
  <Row>
    <Text.H2 class="font-bold">Top 50 Spotify music dataset (2010 to 2019)</Text.H2>
  </Row>
  <Row>
    <LineChart
      query="firstagg"
      x="music_year"
      y="title"
    />
  </Row>
  <Row>
    <Table query="first" />
  </Row>
</View>
```

As you can see in the code above, it works with Svelte. Now, we aim to display analytics with the components available. The `LineChart` component was used because we wanted to display our data as a line chart. Afterward, we added values to the components to work with the parameters in the query files. I also added a table to work with the primary query file.

When you have that code in your `index.html` file, you can now run the project using this command:

```xml
latitude dev
```

By doing this, you'll be redirected to [**localhost:3000**](http://localhost:3000/) from your terminal. Then, you should have this view on your browser displaying your data analytics.


![result image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/be2cswp3q3a0zdn41iha.png)

You can embed your analytics into any website using React or HTML. If you are familiar with React, [**this documentation**](https://docs.latitude.so/react/getting-started/introduction) will guide you on embedding your analytics application with React.

To embed this in any application, you must ensure your Latitude application is deployed on **Latitude Cloud**.

To deploy your application, you need to sign up for Latitude Cloud.

```xml
latitude signup
```

If you have an existing Latitude Cloud account, you can log in to access it.

```xml
latitude login
```

After doing this, you can deploy your application by running the command below.

```xml
latitude deploy
```

Congratulations! You have your Latitude application deployed. 🎉


![thumbs up gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4jkofdu8jmi6m3yrlvj.gif)

Now that we have our Latitude application deployed, the last thing to do is embed the analytics in our Latitude into another website. You can use the **iframe embedding** technique if you're not using React.

This could be done in three ways, depending on what you want to achieve by embedding. It could be embedded directly using parameters in the iframe's value or passing signed parameters.

To embed your analytics directly, you can put this iframe in your code:

```xml
<iframe src="https://app-url.latitude.so/" width="100%" height="400"></iframe>
```

In this context, you need to replace the source, `https://app-url.latitude.so/`, with the actual URL of your deployed application. The iframe code above helps embed your entire analytics into your web application.

That is the easiest way to embed analytics into your application, literally. You can learn about the other methods; [**passing parameters**](https://docs.latitude.so/guides/embed/iframe#passing-parameters) and [**passing signed parameters**](https://docs.latitude.so/views/basics/signed-parameters).

As much as there are tons of tools for visualizing data and embedding analytics into your application, Latitude makes this look very easy. It's not false hype, but the developer experience of Latitude is what makes it a great tool to use. Imagine connecting your data to your front end to integrate analytics into your application seamlessly - it's something you want to try out.

You can learn more about Latitude by going through [**Latitude's documentation**](https://docs.latitude.so/). There are a series of guides that can walk you through everything about Latitude.

## **Conclusion**

This article transitioned from embedded analytics to how you can get started with it easily using Latitude. If you don't understand how to use any embedded analytics tool, Latitude is one you can try out because of its seamlessness and developer experience.

Note that Latitude can be used with React or Svelte. Either way, they’re both great ways to use Latitude.

### **Support us 🙏 ⭐️**

Latitude is open-source; you can contribute to the project as we want to make it more seamless for developers. If you love the project, you can go ahead and give it a star; we appreciate it!

[**Star Latitude on GitHub ⭐️**](https://github.com/latitude-dev/latitude)


![give latitude a star image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hshrv764ke23mkqrprv1.jpeg)

I hope you got the best out of this article. I look forward to hearing your thoughts about using Latitude for your embedded analytics in the comment section. I can't wait to have you read my next article. 👋