Expand my Community achievements bar.

SOLVED

trigger adobeDataLayer push before navigation changes page URL

Avatar

Level 1

Hey team,

 

We are trying to use adobeDataLayer.push() when we make click on a button that redirects to other route, the issue is that when we trigger adobeDataLayer.push() and then we make the redirection adobeDataLayer takes the next page URL, I'm looking for a callback or something to can wait for push method to execute our redirection after. Any suggestion please?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @PedroCi 

in general I would say the Page URL is not really relevant as long as your other analytics dimensions like pageName, props and eVars are set.

If this is relevant to you, you could as a workaround tell your adobeDataLayer push event the correct pageURL and overwrite the analytics attribute with it 

 

 

// assuming you have something like this
adobeDataLayer.push({
  event: "myEvent",
  eventInfo: {
    pageURL: "https://example.com"
  }
})

 

 

 

// and then in the rule that is processing the ACDL push, use an adobe analytics "set variables" action's custom code call this
s.pageURL = event?.message?.eventInfo?.pageURL;

 

 

https://experienceleague.adobe.com/en/docs/analytics/implementation/vars/page-vars/pageurl 

 

Let me know if that works.

 

Side note: this functionality is particularly interesting if you need to clean up the captured URL from unwanted parameters like PII.

Cheers from Switzerland!


View solution in original post

5 Replies

Avatar

Community Advisor

Hi @PedroCi 

what exactly do you mean with "adobeDataLayer takes the next page URL"?

Are we talking about a SPA that pushes a state into the history stack or an actual redirect to a new page?

Because latter will rather cause an aborted request and the new request you see might just be the page view on the following page that is triggered.

General questions: Are you using WebSDK / Edge Network forwarding or the classic AppMeasurement Analytics library?


And which field in your Analytics request are you referring to that is incorrectly set to the new URL?

Normally, the URL itself should not matter much and most fields can anyway be overwritten as needed. 

 

Bottom line, I do not think you need a callback to listen to or delay the navigation accordingly.

 

Cheers

Cheers from Switzerland!


Avatar

Level 1

Hi @bjoern__koth 

I think I'm using the classic library because I'm using window object to trigger adobe analytics methods, yes it's a SPA, I'm trying to track data when the user makes click in a button, this button trigger an internal redirection, I'm sending data to window.adobeDataLayer.push({...data}) but when I checked the data on Adobe experience platform debugger (chrome plugin) I see that the Page URL is different, looks like takes the URL from the next page because probably the push method takes more time than the redirection and takes the page url from window object I think. I'm looking a way to wait for window.adobeDataLayer.push({...data}) and then trigger the redirect method

 

Captura de pantalla 2024-05-28 a la(s) 5.41.39 p. m..png

 

Avatar

Correct answer by
Community Advisor

Hi @PedroCi 

in general I would say the Page URL is not really relevant as long as your other analytics dimensions like pageName, props and eVars are set.

If this is relevant to you, you could as a workaround tell your adobeDataLayer push event the correct pageURL and overwrite the analytics attribute with it 

 

 

// assuming you have something like this
adobeDataLayer.push({
  event: "myEvent",
  eventInfo: {
    pageURL: "https://example.com"
  }
})

 

 

 

// and then in the rule that is processing the ACDL push, use an adobe analytics "set variables" action's custom code call this
s.pageURL = event?.message?.eventInfo?.pageURL;

 

 

https://experienceleague.adobe.com/en/docs/analytics/implementation/vars/page-vars/pageurl 

 

Let me know if that works.

 

Side note: this functionality is particularly interesting if you need to clean up the captured URL from unwanted parameters like PII.

Cheers from Switzerland!


Avatar

Level 1

Hi @bjoern__koth,
yes thank you it's almost done, we are using this script but always returns the page.url because it's always find it in the array, exists a way to get the current index?

 

function getPageURL() {
  //Check if adobeDatLayer exists or has any enteries
  if(window.adobeDataLayer && window.adobeDataLayer.length >0){
    //Loop through adobeDataLayer to find most recent page object with url property
    for (var i = window.adobeDataLayer.length-1 ; i>= 0; i--) {
      if(window.adobeDataLayer[i].page && window.adobeDataLayer[i].page.url) {
        return window.adobeDataLayer[i].page.url;
      }
    }
  }

  // Default to current window location URL if not found in dataLayer
  return window.location.href;
}
return getPageURL();

 

Avatar

Level 1

Now it's working, I'm taking the last index, thank you very much @bjoern__koth 

function getPageURL() {
  //Check if adobeDatLayer exists or has any enteries
  if(window.adobeDataLayer && window.adobeDataLayer.length >0){
    var lastIndex = window.adobeDataLayer.length - 1;
    if(window.adobeDataLayer[lastIndex].page && window.adobeDataLayer[lastIndex].page.url) {
      return window.adobeDataLayer[lastIndex].page.url;
    }
  }

  // Default to current window location URL if not found in dataLayer
  return window.location.href;
}
return getPageURL();