2 deleted 52 characters in body
source | link

What I came up with to help this is a custom Google Analytics Task that listens for events and makes sure the payload contains a page location and title.

//Modify the payload if needed before sending data to Google Analytics
ga(function(tracker){
    var originalSendHitTask = tracker.get('sendHitTask'); //Grab a reference to the default sendHitTask function.

    tracker.set('sendHitTask', function(model){
        //Always make sure events have the page location and title associated with them (in case of session timout)
        if ( model.get('hitType') === 'event' ){
            if ( !model.get('location') ){
                modeltracker.setsend('location', window.location.href'pageview');
            }

        //Send a new pageview if ( !model.get('title') ){
           the event does not have model.set('title',contextual documentdata.title);
            }
        }

        originalSendHitTask(model); //Send the payload to Google Analytics
    });
});

This won't update all field values (which would be nice) like a pageview would, so I've thought about checking if the location is empty and if so sending a new pageview first and then the event...

What I came up with to help this is a custom Google Analytics Task that listens for events and makes sure the payload contains a page location and title.

//Modify the payload if needed before sending data to Google Analytics
ga(function(tracker){
    var originalSendHitTask = tracker.get('sendHitTask'); //Grab a reference to the default sendHitTask function.

    tracker.set('sendHitTask', function(model){
        //Always make sure events have the page location and title associated with them (in case of session timout)
        if ( model.get('hitType') === 'event' ){
            if ( !model.get('location') ){
                model.set('location', window.location.href);
            }

            if ( !model.get('title') ){
                model.set('title', document.title);
            }
        }

        originalSendHitTask(model); //Send the payload to Google Analytics
    });
});

This won't update all field values (which would be nice) like a pageview would, so I've thought about checking if the location is empty and if so sending a new pageview first and then the event...

What I came up with to help this is a custom Google Analytics Task that listens for events and makes sure the payload contains a page location and title.

//Modify the payload if needed before sending data to Google Analytics
ga(function(tracker){
    var originalSendHitTask = tracker.get('sendHitTask'); //Grab a reference to the default sendHitTask function.

    tracker.set('sendHitTask', function(model){
        //Always make sure events have the page location and title associated with them (in case of session timout)
        if ( model.get('hitType') === 'event' ){
            if ( !model.get('location') ){
                tracker.send('pageview'); //Send a new pageview if the event does not have contextual data.
            }
        }

        originalSendHitTask(model); //Send the payload to Google Analytics
    });
});

This won't update all field values (which would be nice) like a pageview would, so I've thought about checking if the location is empty and if so sending a new pageview first and then the event...

1
source | link

What I came up with to help this is a custom Google Analytics Task that listens for events and makes sure the payload contains a page location and title.

//Modify the payload if needed before sending data to Google Analytics
ga(function(tracker){
    var originalSendHitTask = tracker.get('sendHitTask'); //Grab a reference to the default sendHitTask function.

    tracker.set('sendHitTask', function(model){
        //Always make sure events have the page location and title associated with them (in case of session timout)
        if ( model.get('hitType') === 'event' ){
            if ( !model.get('location') ){
                model.set('location', window.location.href);
            }

            if ( !model.get('title') ){
                model.set('title', document.title);
            }
        }

        originalSendHitTask(model); //Send the payload to Google Analytics
    });
});

This won't update all field values (which would be nice) like a pageview would, so I've thought about checking if the location is empty and if so sending a new pageview first and then the event...