4

My clients have put in their website my web-widget, implemented as <iframe> showing a button. I want to track analytics around my clients websites that implement my web widget: how many clicks, and from which websites.

So the <iframe> has src pointing to an html file on mywebiste.com, indeed the html content that shows the button is on my host service and I have access to the code of course.

The widget is used by 3rd party websites. They import my script that put the <iframe> in their webpage. So parent 3rdparty.com has <iframe> pointing to mywebiste.com.

Now, I have put in each html pointed by <iframe>, the GoogleTag integration. I've created a custom-event "click" as trigger, and a "tag" that creates a "GA4 event" to analytics.

If I put these widgets on mywebiste.com as test, triggers and events work and I see on analytics. If I put this widget on 3rdparty.com, triggers and events don't work. And I can't debug GTAG because is not my website.

Why? Theoretically, inside the iframe there is an independent page, that should send the event to my GoogleTag container, with fields "Hostname" as 3rdparty.com.

This is the example code: Besides using the trigger "element click", I also manually push dataLayer to trigger the custom event.

    <!--  3rdparty.com/index.html -->
    <html>
       <!--  I pass hostname as param -->
     <iframe src="https://mywebiste.com/widget.html?hostname=3rdparty.com"></iframe>
    </html>
    <!-- mywebiste.com/widget.html -->
    <html>
     <head>
       <!-- import Google Tag manager -->
     </head>
     <body>
       <button onclick="fireEvent()">Click me!</button>

       <script>
        function fireEvent()
         {
          var hostname = getUrlParamValue('hostname'); // = 3rdparty.com
          window.dataLayer = window.dataLayer || [];
          var event = {'event': 'button_click', 'Page Hostname' : hostname};
          var console.log(event); //this is showed on 3rd party website console
          window.dataLayer.push(event);
          }
       </script>
     </body>
    </html>

I would like to have stats about clicks of the button, with the hostname source details (3rdparty.com)

DeLac
  • 1,068
  • 13
  • 43

2 Answers2

0

If you are using the default/standard Google Analytics tag in GTM, its cookies only work in first-party context. They are subject to SameSite attribute. Your widget won't be able to access the GA cookie(s) and GA won't send any events then.

This can be fixed.

For GA UA add a field "cookieFlags" with the value "samesite=none;secure" to your "Google Analytics Settings" variable in GTM (More Settings, Fields to Set). developers.google.com

For GA4 add a field "cookie_flags" with the value "samesite=none;secure" to your "Google Analytics 4 Configuration" tag in GTM (Fields to Set). developers.google.com

TimW
  • 516
  • 2
  • 7
  • done it, but nothing changed. Just as recap, in this case the 1st party context is the one of my client, that embeds my widget in his iframe. In my page (embedded) I import gtag with my tracking code, and I send the event to my analytics. but I don't receive anything on my analytics – DeLac Aug 10 '23 at 12:52
0

TimW's answer is correct, but applies to the Google Tag (gtag) rather than Google Tag Manager (gtm).

To set GA4 cookie_flags field in gtm I followed the App+Web Configuration tag screenshot in Simo Ahava's The new cookieFlags Setting in Google Analytics.

Here are my exact steps to configure cookie_flags in gtm:

  1. Log into Tag Manager (gtm)
  2. Selector your Container
  3. In the left-hand menu choose Tags
  4. Click on the Google Analytics tag you want to configure (the tag Type should be Google Analytics: GA4 Configuration)

GTM workspace, Tags navigation, indicating Tags configuration to select for editing

  1. Click on the Tag Configuration

Click on the Tag Configuration to edit the tag

  1. Click on Fields to Set to enable editing
  2. Add a new row

Click Add Row to add a new field

  1. Add cookie_flags configuration:
Field Name: cookie_flags

     Value: samesite=none;secure

You can add more properties to the Value, separated by a semi-colon (e.g. samesite=none;secure;max-age=7200).

Here is the final Tag Configuration with the cookie_flags field:

Showing the final Tag Configuration with cookie_flags field


Difference between gtm and gtag

gtm

  • is a generic tag manager
  • it can be used to deliver Google products (e.g. GA4)
  • but can also be used to deliver third-party tags
  • in gtm the cookie configuration field name is cookie_flags

gtag

  • is a tag manager specifically for Google products (e.g. GA4, Google Ads, Campaign Manager, etc.)
  • in gtag the cookie configuration field name is cookieFlags

Quoting from the gtag docs:

The Google tag (gtag.js) is a single tag you can add to your website to use a variety of Google products and services (e.g., Google Ads, Google Analytics, Campaign Manager, Display & Video 360, Search Ads 360). Instead of managing multiple tags for different Google product accounts, you can use the Google tag across your entire website and connect the tag to multiple destinations.

Sly_cardinal
  • 12,270
  • 5
  • 49
  • 50
  • just added `cookie_flafs`as `samesite=none;secure`. The event is showed on the console of my client website, but on analytics I don't see anything – DeLac Aug 10 '23 at 12:37
  • You could try using the debug tools discussed in this video: [DebugView in GA4](https://www.youtube.com/watch?v=dNsCUnUOlgE). Otherwise, you'll need to ask a new StackOverflow question with more information. – Sly_cardinal Aug 11 '23 at 00:00