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

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

  • April 7, 2019
  • 12 replies
  • 10636 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

adilk
adilkAuthor
New Participant
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
New Participant
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