[Customer Pixel] Configure Google Ads Conversion Tracking for View Item Events on Shopify
Learn how to set up Google Ads conversion tracking for view item events on your Shopify store using Google Tag Manager and a customer pixel. Boost your ecommerce insights with this guide.
Shahzada Ali Hassan
1 min read

Check your site's tracking
Enter your website to see how good its tracking is, and whether Meta is likely restricting it.
Run a free auditview_item.js
1// Initialize the dataLayer
2window.dataLayer = window.dataLayer || [];
3(function (w, d, s, l, i) {
4 w[l] = w[l] || [];
5 w[l].push({ "gtm.start": new Date().getTime(), event: "gtm.js" });
6 var f = d.getElementsByTagName(s)[0],
7 j = d.createElement(s),
8 dl = l != "dataLayer" ? "&l=" + l : "";
9 j.async = true;
10 j.src = "https://www.googletagmanager.com/gtm.js?id=" + i + dl;
11 f.parentNode.insertBefore(j, f);
12})(window, document, "script", "dataLayer", "GTM-EXAMPLE");
13function logEventToConsole(dataLayerEvent) {
14 const customStyle01 =
15 "color: #FFFF00; background-color: #000000; font-size: 10px; font-weight: bold; padding: 2px 0;";
16 console.log("%cDataLayer Event: view_item ", customStyle01, dataLayerEvent);
17}
18analytics.subscribe("product_viewed", (event) => {
19 const getEventData = (obj, path, fallback = "") => {
20 return (
21 path.split(".").reduce((acc, part) => {
22 if (acc && part.includes("[")) {
23 const [key, index] = part.replace("]", "").split("[");
24 return acc[key] ? acc[key][index] : undefined;
25 }
26 return acc ? acc[part] : undefined;
27 }, obj) || fallback
28 );
29 };
30 const page_data = {
31 hostname: getEventData(event, "context.document.location.hostname"),
32 location_query_string: getEventData(event, "context.document.location.href"),
33 path: getEventData(event, "context.document.location.pathname"),
34 referrer: getEventData(event, "context.document.referrer"),
35 page_title: getEventData(event, "context.document.title"),
36 url: getEventData(event, "context.document.location.href"),
37 };
38 const user_data = {
39 id: event.clientId,
40 language: getEventData(event, "context.navigator.language"),
41 userAgent: getEventData(event, "context.navigator.userAgent"),
42 };
43 const event_data = {
44 timestamp: event.timestamp || "",
45 id: event.id || "",
46 };
47 const ecommerce_data = {
48 currency: getEventData(event, "data.productVariant.price.currencyCode"),
49 value: getEventData(event, "data.productVariant.price.amount"),
50 items: [
51 {
52 item_id: getEventData(event, "data.productVariant.product.id"),
53 item_name: getEventData(event, "data.productVariant.product.title"),
54 item_brand: getEventData(event, "data.productVariant.product.vendor"),
55 item_category: getEventData(event, "data.productVariant.product.type"),
56 price: getEventData(event, "data.productVariant.price.amount"),
57 item_variant: getEventData(event, "data.productVariant.title"),
58 item_list_name: "Product Page",
59 index: 0,
60 product_id: getEventData(event, "data.productVariant.id"),
61 product_image: getEventData(event, "data.productVariant.image.src"),
62 product_url: getEventData(event, "data.productVariant.product.url"),
63 product_untranslatedTitle: getEventData(event, "data.productVariant.untranslatedTitle"),
64 product_sku: getEventData(event, "data.productVariant.sku"),
65 quantity: 1,
66 },
67 ],
68 };
69 const dataLayerEvent = {
70 event: "view_item",
71 user_data: user_data,
72 page_data: page_data,
73 event_data: event_data,
74 ecommerce: ecommerce_data,
75 };
76 const newUrl = new URL(dataLayerEvent.page_data.location_query_string, window.location.origin);
77 const newTitle = dataLayerEvent.page_data.page_title;
78 if (newUrl && newTitle) {
79 history.pushState(null, newTitle, newUrl.toString());
80 }
81 dataLayer.push({ ecommerce: null });
82 window.dataLayer.push(dataLayerEvent);
83 logEventToConsole(dataLayerEvent);
84});

