Skip to content

Instantly share code, notes, and snippets.

@jasonhdavis
Last active February 11, 2020 16:31
Show Gist options
  • Save jasonhdavis/3cd84f343acbbff0b69fae4f972b6133 to your computer and use it in GitHub Desktop.
Save jasonhdavis/3cd84f343acbbff0b69fae4f972b6133 to your computer and use it in GitHub Desktop.
Send Segment "Page Viewed" actions on subsequent page loads
<!-- Place this in the Apperance -> Custom Include Tags Page -->
<!-- ReadMe.io glue script for Segment -->
<script type="text/javascript">
// Content is loaded via ajax - this means normal js embed will not work.
// Instead, we check for a completed ajax event of type GET
// Similarly, we need to refresh on-page variables
// This event is fired a half second after document ready to give title tag time to refresh
$(document).ajaxComplete(function(event,xhr,settings){
//ReadMe.io updates the document title after page load. Delay sending the Segment event by a half second to capture the updated title
function segmentPageView(){
analytics.track("Page Viewed", {
"name": document.title,
"title": document.title,
"url": document.URL,
"referrer": document.referrer,
});
}
if(settings.type == "GET"){
$(document).ready(function() {
setTimeout(segmentPageView, 500);
});
}
});
</script>
<!-- Include your custom tracking script here to capture "first load" -->
<script src="YOUR_SEGMENT_JS" async></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment