These are chat archives for canjs/canjs

14th
Feb 2017
Marc Godard
@MarcGodard
Feb 14 2017 13:04

I am trying to track routes, I want to call google analytics when a route is changed.

I created the following js file..

import can from 'can';
import 'can/map/define/';
import 'can/route/pushstate/';

export default can.Control({
    'route': function() {
        // Matches every routing change, but gets passed no data.
        console.log("Something changed!")
    },
    ':page route': function(data) {
        console.log("page change", data);
        // Matches routes like #!todos/5,
        // and will get passed {id: 5} as data.
    },
    ':page/:item': function(data) {
        console.log("page change", data);
        // Matches routes like #!recipes/5,
        // and will get passed {id: 5, type: 'recipes'} as data.
    }
});

Then I import it into my app viewModal like this:

import 'app/utils/routing';

This doesn't work. I tried putting it on the bottom of the app view model with new Router(window); like this page has https://v2.canjs.com/docs/can.Control.route.html However that also didn't work.

Could someone help me with what I am missing?

Gira Minus
@gKreator
Feb 14 2017 13:25
How to shim an AMD module using stealjs 1?
Kevin Phillips
@phillipskevin
Feb 14 2017 15:02
@MarcGodard if you’re using done-autorender, I don’t think you can use Control based routing
all of your route properties will be on your appViewModel already
so you can just use setters there
page: {
    set function(val) {
        console.log(‘page changed’, val);
        return val;
    }
}
should be the same as steal 0.16
Marc Godard
@MarcGodard
Feb 14 2017 15:25
@phillipskevin thanks, will play with that.
Marc Godard
@MarcGodard
Feb 14 2017 16:25
@phillipskevin It doesn't seem to work for subpages...
        page: {
            set(newVal) {
                if (newVal == 'listing') {
                    ga('set', { page: newVal + '/' + this.attr("item") , title: this.attr('title') });
                    ga('send', 'pageview');
                } else {
                    ga('set', { page: newVal, title: this.attr('title') });
                    ga('send', 'pageview');
                }
                return newVal;
            }
        },
Kevin Phillips
@phillipskevin
Feb 14 2017 16:26
there will be different events for the other properties
Marc Godard
@MarcGodard
Feb 14 2017 16:37
I got it to work by using can.route.attr('item')
However, now this set property make my url domain.com/home/ instead of just domain.com/
Marc Godard
@MarcGodard
Feb 14 2017 16:45
@phillipskevin I really only need to track subpages... so it seems to work fine when I do the
item: { set(val) {} }
Kevin Phillips
@phillipskevin
Feb 14 2017 16:47
yeah, that should work
if that’s all you need
I don’t think that setter should cause your route to change
Marc Godard
@MarcGodard
Feb 14 2017 16:48
Yeah. Kinda disappointed in the route stuff. I hope when I move to donejs 1 it is better
It was because I set a default as 'home' so because of the set value for some reason, it would add it to the URL
Kevin Phillips
@phillipskevin
Feb 14 2017 16:48
if you have suggestions on how we could improve it, please feel free to open issues
yeah, that sounds like a bug, but I’m not sure
Marc Godard
@MarcGodard
Feb 14 2017 16:49
I will once I get into donejs v1.
It wouldn't be fair to report issues on old versions.
My new site is looking real good though. I can't wait to figure out the SSR and all that so I don't run from a build.