Code is duplicating on SPA set up | Community
Skip to main content
New Participant
March 15, 2023
Solved

Code is duplicating on SPA set up

  • March 15, 2023
  • 2 replies
  • 891 views

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.

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 Matthew_Ravlich_ACG

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.

 

Matthew Ravlich | ACG Digital | albertacg.com

2 replies

Perrin_Ennen
New Participant
March 16, 2023

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

Matthew_Ravlich_ACG
Matthew_Ravlich_ACGAccepted solution
New Participant
March 15, 2023

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.

 

Matthew Ravlich | ACG Digital | albertacg.com