APIs & Integrations

luke_
Participant

Set document.title when tracking page views

Hello, we would like to track page views but need control over what the document.title appears as in HubSpot.

Is there a way to change the document.title for the HubSpot tracking but keep it the same on our actual app?

Possibly something like:
_hsq.push(['setPath', page]); _hsq.push(['setTitle', title]) _hsq.push(['trackPageView']);

Thank you

2 Replies 2
zwolfson
HubSpot Employee
HubSpot Employee

Set document.title when tracking page views

Hey @LukeP,

The tracking code will use whatever the current value of document.title is so it's not possible for it to be separate from what's in your app.

-Zack

0 Upvotes
matttunney
Top Contributor

Set document.title when tracking page views

I got this to work by saving, overriding resetting the document title as part of a page view function.

 

 

  function pageView(elm) {

    // get the original title
    var orginalTitle = document.title;

    // override the title with the link title attribute
    document.title = elm.title;

    var _hsq = window._hsq = window._hsq || [];
    _hsq.push(['setPath', elm.href.replace(/^.*\/\/[^\/]+/, '')]);
    _hsq.push(['trackPageView']);

    // reset to the original title
    document.title = orginalTitle;

}