COGNOiSe.com - The IBM Cognos Community

IBM Cognos Analytics Platform => Cognos Analytics => Reporting => Topic started by: cwillard on 03 May 2022 11:49:41 AM

Title: Pull the Report Name into the JavaScript used in a Custom Control Page Module
Post by: cwillard on 03 May 2022 11:49:41 AM
I am working on adding the Google Analytics code to my reports to help track usage from our website.  I have created a page module custom control that does work correctly when the report page loads.  The code currently sets the report name manually in the code. I want to have the control dynamically get the report name.  The code for the page module is:

define( ["https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"], function() {

function PageModule()
{};

PageModule.prototype.load = function( oPage )
{
window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX', {
  'page_title' : 'Cognos Report Name'
});
};

return PageModule;
});


I know Cognos has a function that will get the report name when added in a query item. I am trying to get the same report name pulled into the custom control without having to add a unique query item in the report. Is it possible to get the report name using JavaScript, and if so, what code do I need to insert in place of Cognos Report Name?  Any suggestions would be greatly appreciated.
Title: Re: Pull the Report Title into the JavaScript used in a Custom Control Page Module
Post by: dougp on 03 May 2022 05:20:45 PM
I don't think that's how you would do it.  JSON can't contain functions.  But you can set a default value and change it downstream.

My first thought is you can scrape it from the switcher.  Inspecting the switcher, you're looking for something like
<button type="button" role="button" aria-haspopup="true" title="Cognos Report Title" id="com.ibm.bi.glass.common.viewSwitcher" class="button menu switcher"...

Grab the "title" from that and set your page_title property.

And, of course, there's probably a more Cognosy way to do this.  This was just my first thought.

In the Scriptable Reports (https://public.dhe.ibm.com/software/data/sw-library/cognos/mobile/scriptable_reports/index.html) docs, I don't see any good docs relating to anything above the Page object.  The application object holds some hope, though.  This may help:
(new DOMParser()).parseFromString(oPage.application.Document.reportXML, "text/xml").getElementsByTagName("reportName")[0].childNodes[0].nodeValue
Title: Re: Pull the Report Name into the JavaScript used in a Custom Control Page Module
Post by: cwillard on 04 May 2022 05:34:32 AM
Thank you for the reply.  I did edit my initial post to change report title to report name. The report name is what the report is saved under and can also pulled into a report using a query with the reportname() function.  I am hoping there is also a way to get the report name using JavaScript.
Title: Re: Pull the Report Name into the JavaScript used in a Custom Control Page Module
Post by: dougp on 04 May 2022 10:00:15 AM
I'm confused.  Did you try my suggestions?

To break it up into readable bits and place it in your code:


define( ["https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"], function() {

function PageModule()
{};

PageModule.prototype.load = function( oPage )
{
window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
 
  var reportSpec = oPage.application.Document.reportXML;
  var dp = new DOMParser();
  var doc = dp.parseFromString(reportSpec, "text/xml");
  var reportNameTag = doc.getElementsByTagName("reportName")[0];
  var reportName = reportNameTag.childNodes[0].nodeValue
  var config = {"page_title" : ""};
  config.page_title = reportName;
 
  gtag('config', 'G-XXXXXXXXXX', config);
};

return PageModule;
});
Title: Re: Pull the Report Name into the JavaScript used in a Custom Control Page Module
Post by: cwillard on 04 May 2022 03:57:03 PM
Dougp,  I had not tested it before I updated my posting and realized, when I was reading my initial post again, that report title could have meant text title in the report instead of the actual name given to the report in Cognos.  I was able to test your suggestions today and did find the second one provided the desired result. I have included the final code I used in case it is helpful to anybody else who may need to add Google Analytic to their reports.


define( ["https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"], function() {

function PageModule()
{};

PageModule.prototype.load = function( oPage )
{

window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX',
  { 'page_title': (new DOMParser()).parseFromString(oPage.application.Document.reportXML, "text/xml").getElementsByTagName("reportName")[0].childNodes[0].nodeValue }
  );
 
};

return PageModule;
});



The code is for Google Analytics 4 and the G-XXXXXXXXXX needs to be replaced with the correct Google Measurement ID. It is added into a Cognos report as a page module.

Thank you for the help.
Title: Re: Pull the Report Name into the JavaScript used in a Custom Control Page Module
Post by: dougp on 05 May 2022 04:07:52 PM
If you did need to get the text from an object on the page, rather than the actual report name, you could include a configuration object for the page module.  I envision the config would refer to the name of the object that contains the text you want to use.
There's no built-in way to pass a config object to a page module.  Here's what I do:

Add a text item to the page and name it PromptConfiguration (or whatever... This is the name the script refers to.)
Change the text in the text item to your JSON-formatted configuration string (like you would for a custom control module).
Put the text item in a block with box type set to None-Block.


function PageModule() {};

PageModule.prototype.load = function( oPage ) {
log("page " + oPage.name, "PageModule.load" );

this.configContainer = oPage.getControlByName("PromptConfiguration");
var i = 0;
while (!this.configContainer) {
this.configContainer = oPage.getControlByName("PromptConfiguration" + (i++).toString());
if (i > 20) break;
}
if (!this.configContainer) {
alert("The prompt configuration container was not found.\r\nIt must be named \"PromptConfigurationx\", where x is empty or a number between 1 and 20.");
}

this.sConfig = this.configContainer.element.innerHTML;

this.oConfig = JSON.parse(this.sConfig);
};


The names I use are because this one is part of a page module specifically to handle numerous prompt features.  As you can see, I have set this up to allow it to be used on multiple pages in the same report, each having its own configuration object.