Support > Advanced Topics > How to track Search events in Google Tag Manager

How to track Search events in Google Tag Manager

Introduction

A common scenario is that you may want to track Store Locator Search events as part of your Google Adwords or other Conversion and goal tracking process using Google Tag Manager. For example, whenever a visitor searches for a location inside your store locator you could record this as a goal inside Google Adwords.

Each time a Search is conducted inside your store locator, the following function is called by the locator if it exists:

storeLocatorSearchEventHandler(eventData)

A search event is recorded in each of the following scenarios:

  • The 'My Location' button is pressed by the visitor.
  • The visitor is automatically geolocated (if the Customer Geolocation setting is set to 'Automatically locate user' on your Settings page).
  • The visitor searches for an address in the address search field
  • The visitor searches for a location using the location search field (if this is enabled).

Installation

1. To get started, create a new tag.

2. Choose 'Custom HTML' as the type.

3. Paste the following into the HTML field with the AW_CONVERSION and AW_CONVERSION_LABEL values taken from your Google Adwords Conversion tracking console:

<script>
  function storeLocatorSearchEventHandler(eventData) {
    gtag('event', 'conversion', {
      'send_to': 'AW-CONVERSION_ID/AW-CONVERSION_LABEL',
      'transaction_id': ''
    });
  }
</script>


4. You'll want to use a firing trigger of type 'Page View'.

5. Now publish the tag to the page on your website where your store locator is hosted (or to the entire site, either is fine). Search events in your locator should be tracked as Conversions.

Advanced Usage

Additional data about the search event is passed inside the eventData object and this can be used to enhance or provide further detail of the conversion type. Here are the attributes of the eventData object:

  • address: The address that was searched (either the address entered into the input field or the address of the midpoint of the map if the 'My Location' button was used to geolocate the visitor)
  • distance: The distance in kms to the closest location
  • filters: Any filters that were applied
  • map_lat: The latitude of the address
  • map_lng: The longitude of the address
  • nearest_storeid: The StoreID of the closest location
  • nearest_storename: The name of the closest location
  • referrer: The HTTP referrer if available (web page which linked to this page)
  • type: The type of search - either "geolocate" where geolocation (the My Location) button was used or "address" where an address was searched for.
  • uid: Your unique account user ID 
  • url: The URL of the current page