How to use a JS variable in another Custom JS script? | Community
Skip to main content
adilk
Level 4
April 7, 2019
Solved

How to use a JS variable in another Custom JS script?

  • April 7, 2019
  • 12 replies
  • 10646 views

In GTM, it's usually by replacing the var name with {{var name}}. How do I do in this Adobe launch?

I have a custom JS that's simply not reading directly from the console log. However, if I separately declare a Data Element that's a JS variable that reads from performance.timing.requestStart, I can see the value in Cloud Debugger.

How can I apply such variables here for startTime and endTime var in below code?

thanks.

function getPageLoadTime() {

if (typeof(performance) !== 'undefined' && typeof(performance.timing) == 'object') {

var timing = performance.timing;

// fall back to less accurate milestones

var startTime = performance.timing.requestStart;

var endTime = performance.timing.loadEventEnd;

if (startTime && endTime && (startTime < endTime)) {

return (endTime - startTime);

}

}

return 'data not available';

}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Alexis_Cazes_

You do not run your code at the time. It needs to be run at the right time of the page lifecycle.

Ideally you would want to run performance code at unload time that way you know that your page is fully loaded (unless people nevigate before the page is fully loaded...).

We do it in this way:

  • Create an event listener on unload
  • On unload a function is called to get all performance data and store them in cookies. We also store the page name that will be the previous page name.
  • On next page send previous page name and performance data to Adobe Analytics
  • In Adobe Analytics run previous page report and add performance metrics

12 replies

sanmeetw1519854
Level 3
April 8, 2019

Hey Adil,

1) Go to data elements

2) Create a new one with core extension and data element type as custom code

3) open editor

4) Just enter the code without the requirement to put everything as function but still ending with a return statement

5) Call this data element wherever you want in Launch using the "%" sign, it will do something similar to "{{" in GTM

Hope this helps

Sanmeet

adilk
adilkAuthor
Level 4
April 8, 2019

Hi Sanmeet,

I'm not a 100% clear but let's try with an example as it'll make it easier for me:

I have two separate data elements in Adobe Launch:

1. SiteSpeed: fetchStart

JS variable

This pulls the timestamp (including ms) from the browser console log: performance.timing.fetchStart

2. SiteSpeed:loadEventEnd

JS variable

This pulls the timestamp (including ms) from the browser console log: performance.timing.loadEventEnd

If I wanted to create a third data element that showed the timestamp by doing this operation:

SiteSpeed:loadEventEnd - SiteSpeed: fetchStart and return the value as an integer.

Would the third variable be a Custom JS and what would the code look like?

Thanks.

sanmeetw1519854
Level 3
April 8, 2019

Adil,

it will be custom JS, you will use _satellite.getVar(<name of data element>) function in your to get the value of each data element, perform your operation on it and return the final value as output of the JS data element.

Hope this helps.

Sanmeet

adilk
adilkAuthor
Level 4
April 8, 2019

Hi Sanmeet,

In my Custom JS, here's what I have as the code:

return _satellite.getVar('SiteSpeed: loadEventEnd') - _satellite.getVar('SiteSpeed: fetchStart');

When I check Cloud Debugger to know if the var value got passed as an event in a rule that I setup, it shows as blank.

It would be event 56 here - which I have configured to accept numeric values instead of a  counter. What could be the reason for this?

Thanks.

sanmeetw1519854
Level 3
April 8, 2019

An event will add up all the values that you pass to it. You wont be able to achieve the objective.

Pass the value to an eVar. You will need to do this analysis in excel

adilk
adilkAuthor
Level 4
April 8, 2019

Yes, so if event 56 = sum of all the timestamp differences and event event 55 = 1 (which is actually a page load counter) and will be a constant at 1 , I could create a calculated metric in AA that adds up the sum of event 56 and divides it by the sum of event 55 to get an average of load time. I'm following this article for context: https://analyticsdemystified.com/adobe-analytics/measuring-page-load-time-with-numeric-event/ 

What do you think about this approach?

and why isn't event 56 showing the timestamp difference as an integer in cloud debugger?

sanmeetw1519854
Level 3
April 8, 2019

I see.

Refer to this link for setting up numeric event :

Numeric or Currency events in DTM or Launch

adilk
adilkAuthor
Level 4
April 8, 2019

ok, here's how cloud debugger is reading my events after setting up the below in AA report suite settings:

event 55 = counter = page load time denominator, always 1

eventt56 = numeric = %SiteSpeed: Page Load Time%

event57 = numeric = %SiteSpeed: fetchStart%

event58 = numeric = %SiteSpeed: eventLoadEnd%

values in debugger:

event55=1

event56=-1554752941276

event57=1554752941276

event58

--

event 57 is correctly pulling value for performance.timing.fetchStart from the console

event 58 is supposed to pull performance.timing.eventLoadEnd but is coming out as blank, leading to event 56 getting a negative value

event 56 is a Custom code as a data element:

return _satellite.getVar('SiteSpeed: loadEventEnd') - _satellite.getVar('SiteSpeed: fetchStart');

If I made the change in AA report suite settings for event58 as numeric type an hour ago, should it immediately show in Cloud Debugger when I reload the page?

What else could be wrong here.

Alexis_Cazes_
Alexis_Cazes_Accepted solution
Level 10
April 9, 2019

You do not run your code at the time. It needs to be run at the right time of the page lifecycle.

Ideally you would want to run performance code at unload time that way you know that your page is fully loaded (unless people nevigate before the page is fully loaded...).

We do it in this way:

  • Create an event listener on unload
  • On unload a function is called to get all performance data and store them in cookies. We also store the page name that will be the previous page name.
  • On next page send previous page name and performance data to Adobe Analytics
  • In Adobe Analytics run previous page report and add performance metrics
sanmeetw1519854
Level 3
April 9, 2019

What Alexis has suggested is correct. The only downside is you wont get data for the exit pages

I am not exactly sure about your code to execute this. I can suggest best practices, but providing a granular solution will be difficult