Thursday, May 3, 2018

PeopleTools - Using Google Analytics (gtag.js)

In the past I have written about implementing Google Analytics in PeopleSoft.

Using analytics.js
Using ga.js

If we look at the latest script provided by Google Analytics, it uses the new JavaScript tagging framework gtag.js. This framework allows us to send event data to different products (Analytics, AdWords, DoubleClick, etc.) without managing them separately.

More on gtag.js: https://developers.google.com/gtagjs/
Global Tracking Code Snippet: https://developers.google.com/analytics/devguides/collection/gtagjs/

If we want to implement Google Analytics in PeopleTools using this latest framework, we cannot simply take the sample code provided by Google and include it in a HTML object in PeopleTools. If you notice, even in my previous Google Analytics posts, I modified the sample code to make it PeopleTools friendly. This is because the script tags within the sample code (which involves loading an external script) will cause issues with PeopleTools when created as a JavaScript (HTML) object.

So, we need a way to first load the gtag.js (external) script and then execute the subsequent code provided in the sample. Here is a good example of how we can load an external script using javascript.
https://www.nczonline.net/blog/2009/07/28/the-best-way-to-load-external-javascript/

Using this approach, I re-wrote the Google Analytics javascript sample as follows. The loadScript function is very similar to the cskLoadJS function in my JavaScript Injection Framework - CSK_FL_BOOTSTRAP_JS.

Script for reference

Note: You will need to find and replace UA-XXXXXXXX-Y with your GA_TRACKING_ID.

CSK_ANALYTICS_JS JavaScript (HTML) Object


Next we need to find a way to inject this javascript within all PeopleSoft pages. A simple approach would be to add a line of code at the end of a global delivered javascript such as PT_COMMON or PT_UTIL. For example:


Alternatively, we could use a JavaScript Injection Framework as described here:
https://pe0ples0ft.blogspot.com/p/javascript-injection-framework.html

Regardless of whether we use analytics.js or gtag.js, when we setup the Google site configuration, we should only include the hostname in the default URL.

E.g.: If this is our login URL
http://pi023.hcm92.com:8000/psp/ps/EMPLOYEE/HRMS/?cmd=login

The Default URL should be configured as follows:


4 comments:

  1. sorry if this results in a double post but I cannot tell if my previous post worked or not.

    If I inject my JavaScript in both PT_COMMON and PT_UTIL will there be any analytic duplication? It seems that PT_COMMON is used by classic and PT_UTIL is used more for fluid but I'm not sure if there is overlap. I bring it up because our site is mainly fluid and I was not seeing as many counts when I used PT_COMMON and since changing to PT_UTIL, my counts have increased from 2-15 to 100+

    Thanks,

    Tom

    ReplyDelete
    Replies
    1. PT_UTIL is included in PT_COMMON as well as PT_COMMON_FMODE.

      If you want to move your code back to PT_COMMON that is fine. But as you figured, that may only execute in Classic pages. So, you will also need to include it in PT_COMMON_FMODE.

      Also, a good way to troubleshoot is to add console.log messages in your script. Then you can see if the script is executing multiple times, etc.

      Delete
    2. Thanks Sasank. We are using the console debug messages to indicate different environments in our code. Since we migrate our scripts through many different environments, we test the URL for domain names and then trigger the appropriate UA code and metrics are recorded to the applicable GA site. It's working very well and we really appreciate your help on this.

      I'm not sure if you are interested but if you have time, a great and logical next extension of this blog entry would be Google Tag Manager. I'd like to see if it is possible to utilize JavaScript review our Job Posting that a user is reviewing for key text such as job categories and then send Tag information back to our analytics account. This would provide an additional level of detail to our analytics where we could filter our analytics by job categories.

      I imagine it would work similar to the basic Google Analytics approach where we would determine the domain name by reviewing URL attributes and then we would use JavaScript to test page contents for key words and when located, we would report back to google with the appropriate tag information.

      Just some thoughts :D.

      Thanks again for a wonderful blog.

      Tom

      Delete
  2. Hi, can we not simply add the JS as html object in branding header?

    ReplyDelete