How to use Google Analytics in web page?

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
Yukiko Ikeo
Posts: 12
Joined: Fri Jun 09, 2017 11:58 pm

How to use Google Analytics in web page?

Post by Yukiko Ikeo » Wed Aug 01, 2018 3:55 pm

version: v14sp2 EPC142010

Hi. Our customer wants to use Google Analytics in web page.
(https://www.google.com/analytics/web/?hl=en)
Does anyone have the experience of this? I've got runtime error in my test.
What I've done was;

1.
create a web page

2.
put the following code in <head> tag of html page.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/j ... "></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'UA-123161253-1');
</script>

3.
At runtime, the following error is displayed on the browser.

********
LANSA Runtime Error:
The operation isn't supported with this object.

Reported By:
http://192.168.1.3:3080/x42pgmlib/lansa ... 0/lansa.js, line 2
********

Regards, Yukiko Ikeo

jyoung
Posts: 690
Joined: Thu Jan 21, 2016 6:43 am
Location: Oklahoma City, OK USA

Re: How to use Google Analytics in web page?

Post by jyoung » Wed Aug 08, 2018 4:02 am

The issue is that you cannot embed html or script directly into a LANSA web page. You need to use a component that will allow to do that.

The components that immediately come to mind are the HtmlContainer or Widget. Don't think a Web Page (embedded external web page) will work like you want, but it may.

I would try embedding the analytics in a widget, then you can include the widget on your page.

I don't have an analytics account, but perhaps this can get you started.

First, create a widget and add the analytics to the resources
capture1.PNG
capture1.PNG (5.61 KiB) Viewed 2186 times
Then hook up the analytics in the widget implementation, note the onCreateInstance function. This may not be the best place for it. OnRealizeControl may be better, you may need to experiment a bit.

Code: Select all

//----------------------------------------------------------
// PROVIDE A SINGLE JAVASCRIPT FUNCTION TO DEFINE THE WIDGET
//----------------------------------------------------------

function( PROTOTYPE, WIDGET )
{
  //--------------------------------------------------------
  // WIDGET-INTERFACE FUNCTIONS (CALLED FROM THE VL-RUNTIME)
  //--------------------------------------------------------

  //
  // 'onCreateInstance' - gets called when LANSA creates an instance of the widget.
  //
  PROTOTYPE.onCreateInstance = function()
  {
    // Provide code to initialize the instance, FOR EXAMPLE
    //this.Caption = 'My Widget';
    window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('send', 'pageview');
  }

  //
  // 'onRealizeControl' - gets called when LANSA creates a visual representation of the widget.
  //
  // Parameters:
  //
  // - parentDiv : the div that's been created as a container for this control.
  //
  PROTOTYPE.onRealizeControl = function( parentDiv )
  {
    // Provide the code to visualize the widget, FOR EXAMPLE...
    //parentDiv.appendChild( document.createTextNode( this.Caption ) );
  }

  //
  // 'onSizeChanged - gets called when the widget changes size.
  //
  PROTOTYPE.onSizeChanged = function()
  {
     // The widget might need to redraw itself.
  }

  //-------------------------------
  // WIDGET-PROPERTY IMPLEMENTATION
  //-------------------------------

  //-----------------------------
  // WIDGET-METHOD IMPLEMENTATION
  //-----------------------------

  // Done
  return WIDGET.Completed;
}
Then drag the widget onto your page.

Hope this helps,
Joe

Yukiko Ikeo
Posts: 12
Joined: Fri Jun 09, 2017 11:58 pm

Re: How to use Google Analytics in web page?

Post by Yukiko Ikeo » Thu Aug 09, 2018 12:30 pm

Hi Joe,

Thank you for your reply.
Using the widget, I finally succeed to implement Google analystics in Web page!

I need to do extra action except for your introduction.
For [Resource] tab of the Web page, I needed to add External Resource as below.
https://www.googletagmanager.com/gtag/j ... 23161253-1

Anyway, your advise was greate help for me.
Thank you so much!

Best regards, Yukiko Ikeo

Post Reply