APIs & Integrations

lbailey5588
Membre

Is it possible to wait for trackPageView before redirecting to an external page?

Hi - We have a WP page that loads the Hubspot tracking script via the WP plugin.

But sometimes the page needs to instantly redirect to an external website based on some JS we execute after page load.

It seems the hubspot script almost never executes, and we miss the majority of pageviews tracked by hubspot.  We're using the wordpress plugin and it adds *asynchronous* and *defer* to the hubspot script, which is probably causing the issue.

So we tried adding a bit of javascript to force a trackPageView *before* we do the redirect. It executes after the page finishes loading. See snippet below.  

Any way we can guarantee the trackPageView makes it to hubspot before the redirect?


------------
window.onload = function() {

// some logic here/do-stuff

// logic decides to instantly redirect

var _hsq = window._hsq = window._hsq || [];
// Track the page view for the new page
_hsq.push(['setPath', '/blahblahblah']);
_hsq.push(['trackPageView']);

//redirect
window.location.href = "https://www.somewhere.com";

}

0 Votes
3 Réponses
lbailey5588
Membre

Is it possible to wait for trackPageView before redirecting to an external page?

// Track the page view for the new page

var _hsq = window._hsq = window._hsq || [];
_hsq.push(['setPath', '/etc']);
_hsq.push(['trackPageView']);  <----  WAIT for hubpot to get the data before doing anything else

 

I want to do this, but must be certain hubspot receives the data before the code continues.    Pretty sure it does all its work asynchronously.

 

Tried using await, but doesnt work.

 

Anyway to force a synchronous trackPageView?

 

 

0 Votes
Jaycee_Lewis
Gestionnaire de communauté
Gestionnaire de communauté

Is it possible to wait for trackPageView before redirecting to an external page?

Hi @lbailey5588  👋 Thanks for your question. And for including those details 🙌  Let's see if we can help get the conversation started.

 

Hey, @Teun @miljkovicmisa @BarryGrennan @JVallejo7, do you have any suggestions for @lbailey5588 or tips on what they could provide the community to help understand their issue? 

 

Thank you very much! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Votes
JVallejo7
Contributeur

Is it possible to wait for trackPageView before redirecting to an external page?

Sorry, i have no idea 😥

0 Votes