Expand my Community achievements bar.

Join Cristinel Anastasoaie, Timothy Furlow, and Brent Kostak, WEDNESDAY, 2/28/24 @8am PT for the next Adobe Target Community Q&A Coffee Break on topics from the recent High Performance Recommendations Webinar PT 2 of the ongoing Target Personalization Maturity Webinar Series
SOLVED

Code is duplicating on SPA set up

Avatar

Level 1

We have a new site which now uses a SPA set up, so Target has followed suit.

 

The issue I'm having is when I inject my code via Target, it works fine on first load, but if the user navigates back to that page/view, the code will be injected again (and the original injection is still in the DOM)

 

Is there a way to get around this? I just want one injection.

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi @kamranali,

 

I am unsure why this is happening with the Target for SPA, but you can add some code that only injects your code if it still needs to be added. In your custom code within Target, add a flag such as this example:

  // Check if code has already been loaded
  if (window.offerLoaded === true) {
    return;
  }
  
  // Set flag to true to indicate code has been loaded
  window.offerLoaded = true;

 

This should prevent the code from injecting twice from your specific activity. 

I suggest opening a client care ticket and having them see if they can provide you with a more permanent fix, so you do not need to add a flag to each activity/custom code.

 

email to open a ticket: ttclientcare@adobe.com 

 

I hope that helps.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 4

Hi @kamranali,

 

I am unsure why this is happening with the Target for SPA, but you can add some code that only injects your code if it still needs to be added. In your custom code within Target, add a flag such as this example:

  // Check if code has already been loaded
  if (window.offerLoaded === true) {
    return;
  }
  
  // Set flag to true to indicate code has been loaded
  window.offerLoaded = true;

 

This should prevent the code from injecting twice from your specific activity. 

I suggest opening a client care ticket and having them see if they can provide you with a more permanent fix, so you do not need to add a flag to each activity/custom code.

 

email to open a ticket: ttclientcare@adobe.com 

 

I hope that helps.

Avatar

Community Advisor

Hi @kamranali,

I agree with @Matthew_Ravlich_ACG. In the code you have to check if the offer has already been injected or not - and react accordingly.

 

Best regards