Skip to content

Commit

Permalink
TrackingConsent: trackPageView changes
Browse files Browse the repository at this point in the history
- add a `trackPageView` public static method for easy page tracking on single-page apps
- set trackPageView by default to track first page view
  • Loading branch information
mraveux committed Aug 1, 2020
1 parent 1980f33 commit 13fefd4
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/components/TrackingConsent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class TrackingConsent extends Vue {
validator: (options) => 'setSiteId' in options,
default: () => ({
setTrackerUrl: TrackingConsent.DEFAULT_TRACKER_URL,
trackPageView: null,
}),
})
public options: {
Expand Down Expand Up @@ -208,6 +209,24 @@ class TrackingConsent extends Vue {
});
}
/**
* trackPageView - allow you to track a new page view. Usefull for single-page apps that use history manipulation
*
* @param {number} generationTimeMs - Time that took the new route to load. Usefull if lazy-loading routes
*/
public static trackPageView(
options?: {
generationTimeMs?: number,
customUrl?: string,
}
) {
if (options.customUrl) TrackingConsent._paq.push(['setCustomUrl', options.customUrl]);
TrackingConsent._paq.push(['deleteCustomVariables', 'page']);
TrackingConsent._paq.push(['setGenerationTimeMs', options.generationTimeMs || 0]);
TrackingConsent._paq.push(['trackPageView']);
}
private static _setCookie(
cookieName: string,
cookieValue: string,
Expand Down Expand Up @@ -372,9 +391,10 @@ class TrackingConsent extends Vue {
}
/* set setTrackerUrl to default value if not set */
if (!this.options.setTrackerUrl) {
this.options.setTrackerUrl = TrackingConsent.DEFAULT_TRACKER_URL;
}
if (!this.options.setTrackerUrl) this.options.setTrackerUrl = TrackingConsent.DEFAULT_TRACKER_URL;
/* set trackPageView if not set, to track the first page view */
if (!this.options.trackPageView) this.options.trackPageView = null;
/* Cycle through options and set them */
Object.keys(this.options).forEach((k) => {
Expand Down

0 comments on commit 13fefd4

Please sign in to comment.