Module: Ahoy::Helper

Defined in:
lib/ahoy/helper.rb

Instance Method Summary collapse

Instance Method Details

#amp_event(name, properties = {}) ⇒ Object



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
# File 'lib/ahoy/helper.rb', line 3

def amp_event(name, properties = {})
  url = Ahoy::Engine.routes.url_helpers.events_url(
    url_options.slice(:host, :port, :protocol).merge(
      name: name,
      properties: properties,
      screen_width: "SCREEN_WIDTH",
      screen_height: "SCREEN_HEIGHT",
      platform: "Web",
      landing_page: "AMPDOC_URL",
      referrer: "DOCUMENT_REFERRER",
      random: "RANDOM"
    )
  )
  url = "#{url}&visit_token=${clientId(ahoy_visit)}&visitor_token=${clientId(ahoy_visitor)}"

   "amp-analytics" do
     "script", type: "application/json" do
      json_escape({
        requests: {
          pageview: url
        },
        triggers: {
          trackPageview: {
            on: "visible",
            request: "pageview"
          }
        },
        transport: {
          beacon: true,
          xhrpost: true,
          image: false
        }
      }.to_json).html_safe
    end
  end
end