pageview.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. var request = require("request");
  2. var qs = require("querystring");
  3. var uuid = require("uuid");
  4. var should = require("should");
  5. var sinon = require("sinon");
  6. var url = require("url");
  7. var ua = require("../lib/index.js");
  8. var utils = require("../lib/utils.js")
  9. var config = require("../lib/config.js")
  10. describe("ua", function () {
  11. describe("#pageview", function () {
  12. var _enqueue;
  13. beforeEach(function () {
  14. _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function () {
  15. if (arguments.length === 3 && typeof arguments[2] === 'function') {
  16. arguments[2]();
  17. }
  18. return this;
  19. });
  20. });
  21. afterEach(function () {
  22. _enqueue.restore()
  23. });
  24. it("should be available via the #pv shortcut", function () {
  25. var visitor = ua()
  26. visitor.pv.should.equal(visitor.pageview)
  27. });
  28. it("should accept arguments (path)", function () {
  29. var path = "/" + Math.random()
  30. var visitor = ua("UA-XXXXX-XX")
  31. var result = visitor.pageview(path)
  32. visitor._context = result._context;
  33. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  34. result.should.be.instanceof(ua.Visitor);
  35. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  36. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  37. _enqueue.args[0][0].should.equal("pageview");
  38. _enqueue.args[0][1].should.have.keys("dp")
  39. _enqueue.args[0][1].dp.should.equal(path);
  40. });
  41. it("should accept arguments (path, fn)", function () {
  42. var path = "/" + Math.random();
  43. var fn = sinon.spy();
  44. var visitor = ua("UA-XXXXX-XX")
  45. var result = visitor.pageview(path, fn)
  46. visitor._context = result._context;
  47. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  48. result.should.be.instanceof(ua.Visitor);
  49. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  50. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  51. _enqueue.args[0][0].should.equal("pageview");
  52. _enqueue.args[0][1].should.have.keys("dp")
  53. _enqueue.args[0][1].dp.should.equal(path);
  54. fn.calledOnce.should.equal(true, "callback should have been called once");
  55. });
  56. it("should accept arguments (params)", function () {
  57. var params = {
  58. dp: "/" + Math.random()
  59. };
  60. var visitor = ua("UA-XXXXX-XX")
  61. var result = visitor.pageview(params)
  62. visitor._context = result._context;
  63. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  64. result.should.be.instanceof(ua.Visitor);
  65. result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
  66. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  67. _enqueue.args[0][0].should.equal("pageview");
  68. _enqueue.args[0][1].should.have.keys("dp")
  69. _enqueue.args[0][1].dp.should.equal(params.dp);
  70. });
  71. it("should accept arguments (params, fn)", function () {
  72. var params = {
  73. dp: "/" + Math.random(),
  74. empty: null // Should be removed
  75. };
  76. var json = JSON.stringify(params)
  77. var fn = sinon.spy()
  78. ua("UA-XXXXX-XX").pageview(params, fn)
  79. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  80. _enqueue.args[0][0].should.equal("pageview");
  81. _enqueue.args[0][1].should.have.keys("dp")
  82. _enqueue.args[0][1].dp.should.equal(params.dp);
  83. fn.calledOnce.should.equal(true, "callback should have been called once");
  84. JSON.stringify(params).should.equal(json, "params should not have been modified")
  85. });
  86. it("should accept arguments (path, hostname)", function () {
  87. var path = Math.random().toString();
  88. var hostname = Math.random().toString();
  89. ua("UA-XXXXX-XX").pageview(path, hostname);
  90. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  91. _enqueue.args[0][0].should.equal("pageview");
  92. _enqueue.args[0][1].should.have.keys("dp", "dh")
  93. _enqueue.args[0][1].dp.should.equal(path);
  94. _enqueue.args[0][1].dh.should.equal(hostname);
  95. });
  96. it("should accept arguments (path, hostname, fn)", function () {
  97. var path = Math.random().toString();
  98. var hostname = Math.random().toString();
  99. var fn = sinon.spy()
  100. ua("UA-XXXXX-XX").pageview(path, hostname, fn);
  101. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  102. _enqueue.args[0][0].should.equal("pageview");
  103. _enqueue.args[0][1].should.have.keys("dp", "dh")
  104. _enqueue.args[0][1].dp.should.equal(path);
  105. _enqueue.args[0][1].dh.should.equal(hostname);
  106. fn.calledOnce.should.equal(true, "callback should have been called once");
  107. });
  108. it("should accept arguments (path, hostname, title)", function () {
  109. var path = Math.random().toString();
  110. var hostname = Math.random().toString();
  111. var title = Math.random().toString();
  112. ua("UA-XXXXX-XX").pageview(path, hostname, title);
  113. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  114. _enqueue.args[0][0].should.equal("pageview");
  115. _enqueue.args[0][1].should.have.keys("dp", "dh", "dt")
  116. _enqueue.args[0][1].dp.should.equal(path);
  117. _enqueue.args[0][1].dh.should.equal(hostname);
  118. _enqueue.args[0][1].dt.should.equal(title);
  119. });
  120. it("should accept arguments (path, hostname, title, fn)", function () {
  121. var path = Math.random().toString();
  122. var hostname = Math.random().toString();
  123. var title = Math.random().toString();
  124. var fn = sinon.spy()
  125. ua("UA-XXXXX-XX").pageview(path, hostname, title, fn);
  126. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  127. _enqueue.args[0][0].should.equal("pageview");
  128. _enqueue.args[0][1].should.have.keys("dp", "dh", "dt")
  129. _enqueue.args[0][1].dp.should.equal(path);
  130. _enqueue.args[0][1].dh.should.equal(hostname);
  131. _enqueue.args[0][1].dt.should.equal(title);
  132. fn.calledOnce.should.equal(true, "callback should have been called once");
  133. });
  134. it("should allow daisy-chaining and re-using parameters", function () {
  135. var path = "/" + Math.random()
  136. var title = Math.random().toString();
  137. ua("UA-XXXXX-XX").pageview(path, null, title).pageview()
  138. _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for each pageview");
  139. _enqueue.args[0][0].should.equal(_enqueue.args[1][0]);
  140. _enqueue.args[0][1].should.eql(_enqueue.args[1][1]);
  141. })
  142. it("should extend and overwrite params when daisy-chaining", function () {
  143. var path = "/" + Math.random()
  144. var path2 = "/" + Math.random()
  145. var title = Math.random().toString();
  146. var title2 = Math.random().toString();
  147. var foo = Math.random()
  148. ua("UA-XXXXX-XX")
  149. .pageview(path, null, title)
  150. .pageview({
  151. dt: title2,
  152. dp: path2,
  153. foo: foo
  154. }).pageview(path)
  155. _enqueue.calledThrice.should.equal(true, "#_enqueue should have been called three times, once for each pageview");
  156. _enqueue.args[0][1].should.have.keys("dp", "dt")
  157. _enqueue.args[0][1].dp.should.equal(path);
  158. _enqueue.args[0][1].dt.should.equal(title);
  159. _enqueue.args[1][1].should.have.keys("dp", "dt", "foo")
  160. _enqueue.args[1][1].dp.should.equal(path2);
  161. _enqueue.args[1][1].dt.should.equal(title2);
  162. _enqueue.args[1][1].foo.should.equal(foo);
  163. _enqueue.args[2][1].should.have.keys("dp", "dt")
  164. _enqueue.args[2][1].dp.should.equal(path);
  165. _enqueue.args[2][1].dt.should.equal(title2);
  166. })
  167. it("should accept document location instead of page path", function () {
  168. var params = {
  169. dl: "http://www.google.com/" + Math.random()
  170. };
  171. var json = JSON.stringify(params)
  172. var fn = sinon.spy()
  173. ua("UA-XXXXX-XX").pageview(params, fn)
  174. _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
  175. _enqueue.args[0][0].should.equal("pageview");
  176. _enqueue.args[0][1].should.have.keys("dl")
  177. _enqueue.args[0][1].dl.should.equal(params.dl);
  178. fn.calledOnce.should.equal(true, "callback should have been called once");
  179. JSON.stringify(params).should.equal(json, "params should not have been modified")
  180. });
  181. it("should fail without page path or document location", function () {
  182. var fn = sinon.spy()
  183. var visitor = ua()
  184. var result = visitor.pageview(null, fn);
  185. visitor._context = result._context;
  186. result.should.eql(visitor, "should return a visitor that is identical except for the context");
  187. result.should.be.instanceof(ua.Visitor);
  188. result._context.should.eql({}, "the transaction params should not be persisted")
  189. _enqueue.called.should.equal(false, "#_enqueue should have not been called once");
  190. fn.calledOnce.should.equal(true, "callback should have been called once");
  191. fn.args[0][0].should.be.instanceof(Error);
  192. fn.thisValues[0].should.equal(visitor);
  193. });
  194. });
  195. });