Skip to content

Commit

Permalink
fix: context path is not updated on spas (#1313)
Browse files Browse the repository at this point in the history
  • Loading branch information
bardisg authored Aug 17, 2023
2 parents cbe05e1 + e8960dc commit 6d05d30
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
10 changes: 10 additions & 0 deletions packages/analytics-js-integrations/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable import/no-extraneous-dependencies */
import path from "path";
import babel from '@rollup/plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
Expand All @@ -10,6 +11,7 @@ import filesize from 'rollup-plugin-filesize';
import typescript from 'rollup-plugin-typescript2';
import nodePolyfills from 'rollup-plugin-polyfill-node';
import { DEFAULT_EXTENSIONS } from '@babel/core';
import alias from '@rollup/plugin-alias';
import * as dotenv from 'dotenv';
import pkg from './package.json' assert { type: 'json' };

Expand Down Expand Up @@ -68,6 +70,14 @@ export function getDefaultConfig(distName, moduleType = 'cdn') {
__PACKAGE_VERSION__: version,
__MODULE_TYPE__: moduleType,
}),
alias({
entries: [
{
find: '@rudderstack/analytics-js-common',
replacement: path.resolve('../analytics-js-common/src'),
}
]
}),
nodePolyfills(),
resolve({
jsnext: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ class CapabilitiesManager implements ICapabilitiesManager {
}

// Get page properties details
const pageProperties = getDefaultPageProperties();
state.page.path.value = pageProperties.path;
state.page.referrer.value = pageProperties.referrer;
state.page.referring_domain.value = pageProperties.referring_domain;
state.page.search.value = pageProperties.search;
state.page.title.value = pageProperties.title;
state.page.url.value = pageProperties.url;
state.page.tab_url.value = pageProperties.tab_url;
this.getPageProperties();
});

// Ad blocker detection
Expand All @@ -115,6 +108,24 @@ class CapabilitiesManager implements ICapabilitiesManager {
});
}

/**
* Get page properties details to use in event context
*/
// eslint-disable-next-line class-methods-use-this
getPageProperties() {
const pageProperties = getDefaultPageProperties();

batch(() => {
state.page.path.value = pageProperties.path;
state.page.referrer.value = pageProperties.referrer;
state.page.referring_domain.value = pageProperties.referring_domain;
state.page.search.value = pageProperties.search;
state.page.title.value = pageProperties.title;
state.page.url.value = pageProperties.url;
state.page.tab_url.value = pageProperties.tab_url;
});
}

/**
* Detect if polyfills are required and then load script from polyfill URL
*/
Expand Down Expand Up @@ -155,6 +166,10 @@ class CapabilitiesManager implements ICapabilitiesManager {
state.capabilities.isOnline.value = true;
});

globalThis.addEventListener('popstate', () => {
this.getPageProperties();
});

// TODO: add debounced listener for globalThis.onResize event and update state.context.screen.value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ICapabilitiesManager {
externalSrcLoader?: IExternalSrcLoader;
init(): void;
detectBrowserCapabilities(): void;
getPageProperties(): void;
prepareBrowserCapabilities(): void;
attachWindowListeners(): void;
onReady(): void;
Expand Down

0 comments on commit 6d05d30

Please sign in to comment.