How to make use of an ID in meteor iron router? - meteor

I know that I can put :id in the path of my route so that I get a specific URL. Thats what I can get form tutorials I found online. But how can I make use of this feature? What is the benefit?
I assume that the ID is passed internally as like an url parameter, since in the data function seems to be specific return, based on an ID. But I am not sure.
this.route('projectView',{
path:'/projects/:id',
layoutTemplate:'mainLayout',
loginRequired:'entrySignIn',
waitOn:function(){
Meteor.subscribe('customers');
return Meteor.subscribe('projects');
},
data:function(){
Session.set('active_project',this.params.id);
return Projects.findOne({_id:this.params.id});
},

In your example, the path looks like /projects/:id. Under the hood, the router converts the contents of :id into this.params.id which is what you are using in your data hook.
In other words, if the path /projects/abc123 was encountered by the router, it would know that it should use the projectView route and this.params.id should equal abc123 when loading the corresponding data.

Related

Exclude nextjs api url from sentry events

I have nextjs app with sentry. I want to add new api route, for example api/status, but I want to exclude it from being sent to sentry as it will clutter logs really fast and use my qouta.
I did a small research and it seems that there is an array of urls you can exclude from being tracked. It's called denyUrls. Read more. I have tried to add my url to this array, but it still tracks this url as part of events:
Sentry.init({
...
denyUrls: [
/api\/status/i,
],
...
});
Am I configuring something wrong or this array is not for the purpose of filtering everts.
If so, what's the best way to filter those? Other option I found which I will try next is beforeSend but it feels a bit overkill to simply exclude url. denyUrls feels like much better fit for what I am trying to achieve
I had the same issue and contacted the support for it. I am directly quoting the support here.
The BeforeSend and DenyUrl are options to filter error events, not transactions. For transaction events, please use the tracesSampler function as described on the page: https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/sampling/#setting-a-sampling-function.
Here is an example to drop all transactions that match a certain name:
tracesSampler: samplingContext => {
if(samplingContext.transactionContext.name == "GET /api/health"){
return 0.0 // never send transactions with name GET /api/health
}
return 0.2 // sampling for all other transactions
}
Note that you might need to customise the function above to better match your scenario.
I hope it will help you ;)
Have a nice day.

Previous page location on IronRouter

Is there a way to get the previous page location before going to the next page in IronRouter?
Is there an event I can use to fetch this information?
Thanks in advance.
Since Iron Router uses the usual History API, you can just use the plain JS method:
history.go(-1);
or
history.back();
Edit: or to check the previous path without following it:
document.referrer;
You can achieve the behavior you want by using hooks.
// onStop hook is executed whenever we LEAVE a route
Router.onStop(function(){
// register the previous route location in a session variable
Session.set("previousLocationPath",this.location.path);
});
// onBeforeAction is executed before actually going to a new route
Router.onBeforeAction(function(){
// fetch the previous route
var previousLocationPath=Session.get("previousLocationPath");
// if we're coming from the home route, redirect to contact
// this is silly, just an example
if(previousLocationPath=="/"){
this.redirect("contact");
}
// else continue to the regular route we were heading to
this.next();
});
EDIT : this is using iron:router#1.0.0-pre1
Apologies for bumping an old thread but good to keep these things up to date saimeunt's answer above is now deprecated as this.location.path no longer exists in Iron Router so should resemble something like the below:
Router.onStop(function(){
Session.set("previousLocationPath",this.originalUrl || this.url);
});
Or if you have session JSON installed (see Session JSON)
Router.onStop(function(){
Session.setJSON("previousLocationPath",{originalUrl:this.originalUrl, params:{hash:this.params.hash, query:this.params.query}});
});
Only caveats with thisis that first page will always populate url fields (this.url and this.originalUrl there seems to be no difference between them) with full url (http://...) whilst every subsequent page only logs the relative domain i.e. /home without the root url unsure if this is intended behaviour or not from IR but it is currently a helpful way of determining if this was a first page load or not

How to get the query parameters in Iron-router?

I am trying to get the query parameters in the url.
There doesn't seem to be an easy way to do this...
which leaves me with the feeling that I must have missed a something in the doc.
Just call
Router.current().params //params is the dict you wanted
in Iron Router 7.1+
Interestingly three answers and no one offered the complete answer.
Iron-Router 1.0.x
From within a route, use:
// URL: http://example.com/page/?myquerykey=true
this.params.query // returns the full query object
this.params.query.myquerykey // returns a particular query value
Similarly, outside of the route (but still inside the client code), and inside your template, use:
// URL: http://example.com/page/?myquerykey=true
Router.current().params.query
Router.current().params.query.myquerykey
Query parameters, not to be confused with parameters passed via the URL.
iron router >= 1.0
A route's query parameters are available as properties of this.params.query.
If your URL looked like:
/posts/5?sort_by=created_at
then this.params.query.sort_by would equal 'created_at'.
iron router < 1.0
A route's query parameters are available as properties of this.params.
If your URL looked like:
/posts/5?sort_by=created_at
then this.params.sort_by would equal 'created_at'.
In Iron Router 1.0.0, you need to use
this.params.query.YOUR_PARAMETER_NAME
to get it
For example, if you route is /xxx/?a=b
this.params.query.a
outputs 'b'
try tihs:
Router.current().params.parametername;
and in router.js file routing must be:
route(routername/:parametername)
Ensure that if you are using Router.go that your first parameter is a template name, and not a path. query parameters are not passed if you specify a path.
Encoded URI undefined Solution:
The better way to get the query parameters object is:
this.request.query.MyParam
Using the suggested option of:
this.params.query.MyParam
Is ok as long as you are not working with encodedURI parameters, when using this option with encodedURI parameter, the parameter will be equal to undefined.
Example below:
{ // console.log(this.params.query)
product: 'Chair',
ip: '172.0.1.183',
message: 'My Little Chair',
request: '100% Discount',
severity: '4',
api_key: 'XXXXX'
}
{ // console.log(this.params.query)
product: 'Chair',
ip: '172.0.1.183',
message: 'My Little Chair',
request: 'undefined', // NOTICE THIS CHANGED TO UNDEFINED!
severity: '4',
api_key: 'XXXXX'
}
Original Query String:
?product=Chair&ip=172.0.1.183&message=My Little Chair&request=100%25%20Discount&severity=4&api_key=XXXXX
You can pass queries like this depending on where you accessing the router:
In the template
{{pathFor 'routeName' query='queryName=queryValue'}}
In the helper
Router.go ('routeName',{},{query: 'queryName=queryValue'}
Note: the empty object between the routeName and the query is if you want to specify any parameters (refer to the full docs to see the difference).
If you would like to pass multiple queries do it like this:
query: 'queryName1=queryValue&queryName2=queryValue'
Don't use spaces and remember to use the & sign.

Using Meteor, is it possible to have "dynamic routes?"

sorry, I'm new to webdev and Meteor and I'm not quite sure of the correct terminology. I am using Meteor-Router to create routes in my Meteor app.
I'm trying to create a test restaurant app, so an entry in my database might be:
name: "Kentucky Fried Chicken"
type: "Fast Food"
On the main page of the app, you see a list of restaurants. But the user can click on any item on that list to get to a more detailed page.
I would rather that the urls don't look like:
/restaurant/123
but more so like:
/fast-food/kentucky-fried-chicken
/japanese/sushi-r-us
/italian/some-italian-restaurant-name
Is this possible to do with Meteor & Meteor-Router? Thank you!
Btw, right now my routes are very simple:
Meteor.Router.add({
'/': 'home',
'/admin': 'admin',
'/403': 'unauthorized'
});
You can use more complex routes than the one you're using now, like this:
Meteor.Router.add({
'/:type/:restaurant': function(type, restaurantName) {
var restaurant = Retaurants.findOne({type: type, name: restaurantName});
Session.set('restaurantFromUrl', restaurant);
// Now your restaurant is in the "restaurantFromUrl" Session
return 'restaurantPage';
}
});
The /:type and /:restaurant will be passed into the callback and be whatever you set them to in your URL. Oh, and you might want to add a /show-restaurant/type/name/ also, else all urls (that aren't set up in other routes) that match the patter "/whatever/url" will try to get a restaurant.
Everything you need to know is here: https://github.com/tmeasday/meteor-router
Oh, and this is just an example. Haven't tested it but it should work.
Current route package for Meteor that most people use is: Iron Router

How to track custom parameter with Google Analytics and utm.gif

The situation
I'd like to use GA to track some serverside operations. That's why I cant make use of the GA JavaScript functions. But, as you might know, you can request the utm.gif right from your server. This already works fine.
The Problem
I'd like to trackt custom parameters. But I have no idea how to add them in the right format to the url-request
This one should do the custom parms. But I didn't get any results in GA.
utme=5(Init*load_success*http://www.mydomain.de)8(userstatus)9(fan)11(2)
Full list of params:
ref ts
utmac UA-XXXXXX-5
utmcc __utma=186215409.1789216404.1265552708.1280074861.1280493144.21;+__utmz=;
utmcs ISO-8859-1
utmdt Button
utme 5(Init*load_success*http://www.mydomain.de)8(mycustomvar)9(mycustomvalue)11(2)
utmfl -
utmhn mydomain.de
utmje -
utmn 1114675642
utmp button
utmr http://www.mydomain.de
utmsc -
utmsr -
utmul de-de
utmwv 4.5.7
not sure what's going wrong, given what you posted, but how about you write out what you want to send the traditional way (with javascript) and put it on a test page. Use firebug or whatever to grab the requested url that's built and compare it to what you have now.
The value of the utme gif Request parameter is encoded by ga.js--it's the only one that is, as far as i know.
Calling __trackEvent is the usual way to set the value of utme. These are client-side events though, which is no doubt why you are trying to set utme directly.
So if you just want to bind 5(Initload_successhttp://www.mydomain.de)8(userstatus)9(fan)11(2) to the variable utme, and you can't rely on user-behavior to trigger that binding, then here's what i suggest:
Pack your data into a 'custom variable' scoped to the page--this way, when the __trackPageview() is called, the value will be set.
Here's the analytics code required in your HTML to implement that:
The method signature for a custom variable:
pageTracker._setCustomVar(slot, // integer between 1 and 5, inclusive (just use '1')
name, // user-defined name for the custom variable
value, // string representing the value for the custom variable
scope, // you want '3' for page-level (an int, not a string though)
);
Within the HTML (order matter, of course):
pageTracker.__setCustomvar(1, "A Name", "A Value", 3);
pageTracker.__trackPageview();
A key point here is that the parameter 'value' can be set dynamically, so for the 'value' parameter, i guess you want to pass in 5(Initload_successhttp://www.mydomain.de)8(userstatus)9(fan)11(2)
Finally, here are the two key sources (Implementation Guide, Usage Guide) on Custom Variables from the GA Team

Resources