File: test_rack-google-analytics.rb

package info (click to toggle)
ruby-rack-google-analytics 1.2.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 164 kB
  • sloc: ruby: 287; makefile: 9
file content (112 lines) | stat: -rwxr-xr-x 3,535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'helper'

class TestRackGoogleAnalytics < Test::Unit::TestCase

  context "Asyncronous" do
    context "default" do
      setup { mock_app tracker: 'somebody' }
      should "show asyncronous tracker" do
        get "/"
        assert_match %r{ga\('create', 'somebody', {}\)}, last_response.body
        assert_match %r{ga\('send', 'pageview'\)}, last_response.body

        assert_match %r{</script></head>}, last_response.body
      end

      should "not add tracker to none html content-type" do
        get "/test.xml"
        assert_no_match %r{ga\('create', 'somebody', {}\)}, last_response.body

        assert_match %r{Xml here}, last_response.body
      end

      should "not add without </head>" do
        get "/bob"
        assert_no_match %r{ga\('create', 'somebody', {}\)}, last_response.body
        assert_match %r{bob here}, last_response.body
      end

      should "redirects" do
        get "/redirect"
        assert_equal 302, last_response.status
      end
    end

    context "with custom domain" do
      setup { mock_app tracker: 'somebody', domain: "railslabs.com" }

      should "show asyncronous tracker with cookieDomain" do
        get "/"
        assert_match %r{ga\('create', 'somebody', {\"cookieDomain\":\"railslabs.com\"}\)}, last_response.body
        assert_match %r{ga\('send', 'pageview'\)}, last_response.body

        assert_match %r{</script></head>}, last_response.body
      end

    end

    context "with enhanced_link_attribution" do
      setup { mock_app tracker: 'happy', enhanced_link_attribution: true }
      should "embedded the linkid plugin script" do
        get "/"
        assert_match %r{linkid.js}, last_response.body
      end
    end

    context "with advertising" do
      setup { mock_app tracker: 'happy', advertising: true }
      should "require displayfeatures" do
        get "/"
        assert_match %r{ga\('require', 'displayfeatures'\)}, last_response.body
      end
    end

    context "with e-commerce" do
      setup { mock_app tracker: 'happy', ecommerce: true }
      should "require ecommerce" do
        get "/"
        assert_match %r{ga\('require', 'ecommerce', 'ecommerce\.js'\)}, last_response.body
      end
    end

    context "with anonymizeIp" do
      setup { mock_app :async => true, :tracker => 'happy', :anonymize_ip => true }
      should "set anonymizeIp to true" do
        get "/"
        assert_match %r{ga\('set', 'anonymizeIp', true\)}, last_response.body
      end
    end

    context "with dynamic tracker" do
      setup do
        mock_app tracker: lambda { |env| return env["misc"] }, misc: "foobar"
      end

      should 'call tracker lambdas to obtain tracking codes' do
        get '/'
        assert_match %r{ga\('create', 'foobar', {}\)}, last_response.body
      end
    end

    context 'adjusted bounce rate' do
      setup do
        mock_app tracker: 'afake', adjusted_bounce_rate_timeouts: [15, 30]
      end
      should "add timeouts to push read events" do
         get "/"
         assert_match %r{ga\('send', 'event', '15_seconds', 'read'\)}, last_response.body
         assert_match %r{ga\('send', 'event', '30_seconds', 'read'\)}, last_response.body
      end
    end

    # context "with custom _setSiteSpeedSampleRate" do
    #   setup { mock_app :async => true, :tracker => 'happy', :site_speed_sample_rate => 5 }
    #   should "add top_level domain script" do
    #     get "/"
    #     assert_match %r{'_setSiteSpeedSampleRate', 5}, last_response.body
    #   end
    # end

  end

end