Mark Ku's Blog

Objective

Based on a previous article, we know that we can integrate custom events with Google Analytics using Google Tag Manager.

This article will demonstrate how to pass event parameters through Google Tag Manager to capture the keywords that users search for on your e-commerce site and integrate this data into GA4 reports.

Prerequisites

First, execute the following front-end code within the search button's event handler on your website

export const pushSearchDataLayer = (searchString: string) => {
  const dataLayer = window.dataLayer || [];
  const pushData = {
    event: 'searchSubmit',
    SearchParam: searchString,
  };

  dataLayer.push(pushData);
};

Next, we need to create a variable to receive the parameter passed from the front-end code. Click Variables > New

Google Tag Manager variables page, New button highlighted
Google Tag Manager variables page, New button highlighted

Click Choose variable type > and select DataLayer

GTM choose variable type dialog with Data Layer Variable highlighted
GTM choose variable type dialog with Data Layer Variable highlighted

Fill in the Data Layer Variable Name. You can use the Datalayer Checker Chrome plugin to find the correct variable name. Then, rename the variable to something easily recognizable, like "DataLayer - searchString".

Google Tag Manager variable configuration for searchString data layer (Datalayer Checker Plugin) Datalayer Checker showing searchString variable value t-shirt

Go back to Tags > and edit the "GA4 Event - searchSubmit" custom event you created earlier

Google Tag Manager tags list highlighting GA4 searchSubmit event
Google Tag Manager tags list highlighting GA4 searchSubmit event

Now, you can add the variable you created earlier, "DataLayer - searchString", to the Event Parameters

Google Tag Manager adding DataLayer searchString to GA4 event parameters
Google Tag Manager adding DataLayer searchString to GA4 event parameters

Finally, go back to your GA reports > Realtime > Event count > and click on searchString. You will now be able to see statistics on the keywords users have searched for.

GA4 Realtime report showing t-shirt in searchString event
GA4 Realtime report showing t-shirt in searchString event

References

GTM Data Layer - A Basic Introduction

What are the data layer and JSON, and how do they interact with GTM?

New GA4 E-commerce Events: Differences Between Old and New, and Purchase Conversions!

Author

Mark Ku

擁有 10+ 年經驗的資深軟體工程師,現為 AI 應用 Builder,專注於大型平台架構與簡化複雜系統設計,從電商系統到訂閱與收費平台,結合 AI Agent、AI 整合與自動化開發,打造高效率且可持續演進的產品技術基礎。Read More

Found this useful?

The author's free tools, daily podcasts and newsletter are all here.

Mark Ku · This article is licensed under CC BY 4.0. Credit the author and link back to the original when reusing it.

Comments

Subscribe to Newsletter

Subscribe to get new posts delivered instantly — never miss a tech share.

By submitting, you agree to receive emails. You can anytime.

Popular Posts

View all
Mark Ku
··602

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution

Oracle Cloud Always Free Tier: Linux Host and Static IP for a $0 Cloud Solution
Mark Ku
··490

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.

Say Goodbye to Postman's Fee Trap! A Hands-on Guide to Bruno, the Open-Source Git-Native API Testing Powerhouse.
Mark Ku
··333

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki

A Free, Open-Source, Notion-like Knowledge Base — A Complete Guide to Deploying and Backing Up Outline Wiki
Mark Ku
··264

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning

Training Your Own AI Voice: Hardware Requirements, Open-Source Model Comparison, and LoRA Fine-Tuning
Mark Ku
··221

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1

Building an Efficient API Management Platform: Deploying Kong Gateway from Scratch - Part 1
Mark Ku
··215

Setting Up Samba on Ubuntu to Share Folders with Windows 11

Setting Up Samba on Ubuntu to Share Folders with Windows 11