[Rails] Set Google Analytics conversion tag

What is the goal (measurement of conversion rate)?

With the function of Google Analytics, you can set the index of the website by setting goals such as "member registration", "content registration", and "viewing 5 pages or more". I also set goals on my website (https://www.mini4wg.com/), so I will explain the setting items and implementation methods.

スクリーンショット 2020-10-27 14.05.21.png

Goal setting 1/3

Templates are prepared and it is very convenient. If you specify google analytics, you can also modify the site to support diagrams. However, depending on the screen transition etc., it may be necessary to prepare for the conversion tag.

The first goal is --Revenue --Attracting customers

スクリーンショット 2020-10-27 14.13.49.png

Explanation of goals 2/3

The goal description sets the type of measurement. --Arrival page --Stay time --Number of page views --Event Can be set.

スクリーンショット 2020-10-27 14.25.22.png

Goal details 3/3

The form here will change depending on the type set in the goal description (2/3). If you select an event, you need to send an event from your website. The arrival page, staying time, and page view can be used simply by setting from the Google Analyticw screen.

スクリーンショット 2020-10-27 14.28.03.png

When sending an event in Rails

My site also has a function to register images, but when I want to send it only once at the time of creation to measure conversion, I implemented it as follows.

ProductsController

ruby


class ProductsController < ApplicationController
  =abridgement=
  def create
    @product = current_user.products
                           .build(product_params)
    authorize @product

    if @product.save
      flash[:cv] = true
      redirect_to @product, notice: t('.saved')
    else
      render :new
    end
  end
 =abridgement=
end

views/products/show.html.slim

slim


~~abridgement
- if flash[:cv] == true
  javascript:
    gtag('event', 'created', { 'event_category': 'products', 'event_label': '#{@product.id}'});
~~abridgement

Confirmation method

You can see what kind of event is being sent by inserting the following Chrome Extension to see if the event is actually being sent.

Page Analytics (by Google) --Chrome Web Store https://chrome.google.com/webstore/detail/page-analytics-by-google/fnbdnhhicmebfgdgglcdacdapkcihcoh/related?hl=ja

Finally

By setting goals, it is very useful to have indicators to help improve the site and explain to others.

Recommended Posts

[Rails] Set Google Analytics conversion tag
[Rails] How to introduce Google Analytics [Easy]