2016-10-02 147 views
0

所以我需要更改我的网址,以便谷歌分析可以跟踪它。谷歌分析不会用链接中的“/#/”(散列)来接受它。这就是说,我用角的locationProvider和修订我的应用程序的路由:角URL路由问题

(function() { 
    'use strict'; 

    angular 
    .module('mbapp') 
    .config(routerConfig); 

    /** @ngInject */ 
    function routerConfig($stateProvider, $urlRouterProvider, $locationProvider) { 
    $stateProvider 
     .state('home', { 
     url: '/', 
     templateUrl: 'app/main/main.html', 
     controller: 'MainController', 
     controllerAs: 'main' 
     }); 

    $stateProvider 
     .state('steps', { 
     url: '/steps', 
     templateUrl: 'app/steps/steps.html', 
     controller: 'StepsController', 
     controllerAs: 'steps' 
     });  

    // use the HTML5 History API 
    $locationProvider.html5Mode(true);  

    $urlRouterProvider.otherwise('/'); 
    } 

})(); 

我的网址是罚款,并将其更改为http://website.com/steps,而不是http://website.com/#/steps。但是,现在,如果用户刷新(f5)链接,则会抛出404错误,但不知道为什么。此外,当刷新被称为“http://website/steps#/steps”时,它似乎以某种方式被注入为URL。

对此的任何想法?

非常感谢。

+0

我有同样的问题来解决它我更新的分析onpagechange像在这个http://stackoverflow.com/questions/12989106/handle-urls-with-hash-with-google-analytics –

+0

消化了几种角度分析模块周围......没有这个问题,我自己与基于散列的路由,并也实时监测ga流量 – charlietfl

+0

@AsafHananel - 你的意思是“_trackPageview”? – Mark

回答

0

问题可能出现在服务器端。您必须配置您的服务器,以便使用您的html文件响应每个请求。例如,在快递:

var app = require('express')(); 

app.configure(function(){ 
    // static - all our js, css, images, etc go into the assets path 
    app.use('/assets', express.static('/assets')); 

    app.get('/api/users/:id', function(req, res){ 
    // return data for user.... 
    }); 

    // This route deals enables HTML5Mode by forwarding missing files to the index.html 
    app.all('/*', function(req, res) { 
    res.sendfile('index.html'); 
    }); 
}); 

当你重新载入页面,请求进入应用程序的服务器端,并尝试解析URL,但它可能不能,因为这些路线只存在于客户端的应用程序。

此外,为每个服务器端路由添加/api路由前缀的前缀是个好主意,因此您可以轻松区分客户端路由和服务器端路由。