0

First let me thank everyone who has contributed in my education, it has helped a great deal and I have learned a lot in 7 months.

I have been requested to count every pageview when someone visits a details page. I updated my database to add a pageview column but I am unsure of how to do anything else. I think I should put the code on my details ActionResult on my controller since that is the page I want to be tracked. If this is wrong please correct me.

 public ActionResult Details(int id, int? yearId)
        {
            var dbTest = _testRepository.Id(id);
            var year = yearId == null ? dbTest.LastYear : dbTest.Date.Single(i => i.Id == yearId);
            var vm = new DetailsVM
            {
                Test = _mapper.Map<TestVM>(year)
            };

            return View(vm);
        }

Above is my action result. What it does is collects everything from the database and displays it on a details cshtml page. This code works. How do I get each pageview to count and log into my database using the column name pageview. Please explain your methods so I can learn from it.

Edit:
I have looked into Google Analytics and it isn't what I am looking for. I just want something to use for organization.

I have also included my database model which hopefully provides more information.

public class Test
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public ICollection<TestYear> Years { get; set; }
        [NotMapped]
        public TestYear LastYear {
            get {
                return this.Years.OrderByDescending(i => i.Year).FirstOrDefault();
            }
        }
        public int PageViews { get; set; }
    }
BlowFish
  • 183
  • 3
  • 12
  • If what you really care about is how are customers using my site then you most likely want to look into Google Analytics or a similar service. And if you really want to do using your own code the use global action filters – Divyang Desai Oct 14 '16 at 16:16
  • I have read about using Google Analytics but haven't found a good tutorial or explanation/example on HOW to use it. Or where. – BlowFish Oct 14 '16 at 16:24
  • Have a look on [this](https://www.google.com/analytics/#/section-quote) And [this](http://stackoverflow.com/questions/21832711/implementing-google-analytics-in-mvc4-c) – Divyang Desai Oct 14 '16 at 16:28
  • I have google analytics on my application. However, how do you pull a number from analytics. I need to use the stored number. So if a page as 500 page views on google I need it in the code for example: data-views="google analytics generated number here" – BlowFish Oct 14 '16 at 16:58
  • I don't think google analytics is going to work for me – BlowFish Oct 14 '16 at 18:39
  • Valid point, but think you can also do that with Google-analytics. **How?** read [here](https://developers.google.com/analytics/devguides/config/mgmt/v2/mgmtFeedReference?csw=1#profileFeed). – Divyang Desai Oct 15 '16 at 12:38
  • I think you should search on more about this, and look at [here](http://stackoverflow.com/questions/6033392/google-analytics-api-display-page-views) – Divyang Desai Oct 15 '16 at 12:46

1 Answers1

0

I ended up just making a method in my repository to add a counter to the page-views column in my database every time an id was hit.

public void AddView(int id)
    {
        var view = _context.Tests.Single(i => i.Id == id);
        view.PageViews++;
        _context.SaveChanges();
    }
BlowFish
  • 183
  • 3
  • 12