Connect Google Analytics to your APEX application
- Personal Chao yu
- Jul 1, 2021
- 3 min read
Updated: Jul 7, 2021
this is not a tutorial but rather a note I have for myself.
prerequisite:
Any APEX application you have access to
Google Account
Go to google analytics ( make sure you have google analytics enabled )
Go to the following section of the admin page and click on Create Account

Once Account is created switch to the newly created account by selecting in the drop drpodownlist.(marked in red)
Then you aught to create a new property. Once a new property is created, go to Data Streams.



Go to APEX application builder, find the page you would like to include Google Analytics.
The code snippet is from Web Stream Details from previous page.

That is it! With these steps, you will be able to see some universal statics about your site for this page ( because you only include the google snippet on this page).

How to create Custom Events in Google Analytics:
Say, i want to record all the clicks on these four different buttons. And tell them apart from each other.

- First Create custom events in google:
there are 2 ways of creating custom events



- then create dynamic action for each button.

gtag('event', 'click', {
'event_category' : 'engagement',
'event_label' : 'click_button_tank_btn',
'content_id':'tank'
});
gtag('event', 'click', {
'event_category' : 'engagement',
'event_label' : 'click_button_orderbtn',
'content_id':'order'
});
gtag('event', 'click', {
'event_category' : 'engagement',
'event_label' : 'click_button_jettiesbtn',
'content_id':'jetties'
});
gtag('event', 'click', {
'event_category' : 'engagement',
'event_label' : 'click_button_settingsbtn',
'content_id':'settings'
});
Notice that I used 'content_id' to identify the click event. This parameter is used on Google Analytics to create custom events. Here: =>

2.
You can also create an event directly with scripts, like this =>
gtag('event', 'sample_event', {
'parameter1':'value1',
'parameter2':'value2'
});
Both works. ( however if you create again the same sample_event in google analytics, once it is triggered, it will fire two times )
I also have a small example of search box you might want to track.

let searchVal = apex.item( "P1_NEW" ).getValue();
gtag("event", "search", {
"search_term": searchVal,
"value": searchVal
});
You could also collect the search string and send it to Google Analytics.
-------------------------------------------------------------------------------------------------
Let us look at this particular event.
To view this event. Go to => LIFT CYCLE => Engagement => Events => find search on the bottom list.

All event has its own default parameters ( see right red marker). Let us find Parameter we send to Google,(you could, if you like to create your own parameters, as long as it follow the json string format) search_term and value.



You could easily make a matrix find the most searched items. and make changes to your report and sort base on it.
Another Example :
Say you are making a single page application, where you use a lot of Ajax to swap content or hide and show. And you want to see how often a 'Page' is viewed and how long user stayed on it.
let terminalName = apex.item( "P1_NEW_1" ).getValue();
gtag('event', 'page_view', {
page_title: terminalName,
page_location: '',
page_path: '',
send_to: 'YOU OWN PROPERTY ID'
})
I use this drop-down list to simulate page swap.

-- RealTime

-- LifeCycle : On this page, you can find all page_views and the average engagement time! ( there is a delay between real-time page and LIFE CYCLE page i believe it is one day ... )

So let us recap,
with this set-up, we are able to view default charts and tables. More importantly we are able to monitor custom event and collect custom data. In the next blog, we will try to make sure of the custom event and data and make our own charts and tables.
Comments