joernroeder / piwik-react-router

Piwik analytics component for react-router
https://www.npmjs.org/package/piwik-react-router
MIT License
61 stars 22 forks source link

Incorrect url in Piwik when use hashHistory from react-router #39

Closed regdos closed 7 years ago

regdos commented 7 years ago

Page url address - https://domain.com/context/#/form?lang=en

In Piwik url register as https://domain.com/form?lang=en

I use hashHistory from react-router in my app. piwik-react-router send push(['setCustomUrl', currentPath]), currentPath is calculate as currentPath = urljoin(loc.pathname, loc.search) and for my url currentPath is /form?lang=en

Piwik calculate url https://github.com/piwik/piwik/blob/3.x-dev/js/piwik.js#L3275 and when url is absolute add only schema and host

if (url.slice(0, 1) === '/') {
    return getProtocolScheme(baseUrl) + '://' + getHostName(baseUrl) + url;
}

and i get my incorrect url https://domain.com/form?lang=en

Solving the problem would be for example remove initial slash depending on the parameter.

What do You think about it?

joernroeder commented 7 years ago

have you seen and applied the post in the piwik faq? https://piwik.org/faq/how-to/faq_188/

regdos commented 7 years ago

Yes, but this is not my case because url sended to piwik not contain hash.

After analyze code and testing when i add remove initial slash

currentPath = currentPath.replace(/^\//, '');

before line https://github.com/joernroeder/piwik-react-router/blob/master/index.js#L58 url send to piwik is correct (contain context and hash)

joernroeder commented 7 years ago

if i understand you right, your main problem is the loos of the context path. is that correct? You could add the basename (in your case context) to the history object to prefix the app path.

If all the URLs in your app are relative to some other "base" URL, use the basename option. This option transparently adds the given string to the front of all URLs you use.

https://github.com/ReactTraining/history#using-a-base-url

Alternatively you could proxy the history.listen callback and pass down your modified path via loc.path to set the currentPath via https://github.com/joernroeder/piwik-react-router/blob/master/index.js#L51

hope that helps!

cheers jörn

joernroeder commented 7 years ago

@regdos after thinking about the proxy/modifier today i found other use cases to modify the location (e.g. to add campain parameters or state from outside react). I've ended up adding the functionality of History Modifiers directly into .connectToHistory. Please head over to the docs to see if it works well for you. I am excited about your feedback.

joernroeder commented 7 years ago

@regdos could you resolve the issue? I'm closing this issue for now as the possibility to manipulate the current location before it gets passed to the tracker exists in the module since this version (v0.9.0).

Thanks for opening this issue as it leaded to a new useful feature of this module and feel free to reopen it if you need any help. cheers

LiuuY commented 6 years ago

Come across the same problem, as @joernroeder said, I just pass a modifier to .connectToHistory:

const modifier = function (location) {
    const loc = window.location;
    location.path = `${loc.origin}${loc.pathname}${loc.hash}`;
    return location;
};